001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.control.user.offer.server.command; 018 019import com.echothree.control.user.offer.common.edit.OfferEdit; 020import com.echothree.control.user.offer.common.edit.OfferEditFactory; 021import com.echothree.control.user.offer.common.result.EditOfferResult; 022import com.echothree.control.user.offer.common.result.OfferResultFactory; 023import com.echothree.control.user.offer.common.spec.OfferSpec; 024import com.echothree.model.control.filter.common.FilterKinds; 025import com.echothree.model.control.filter.common.FilterTypes; 026import com.echothree.model.control.filter.server.logic.FilterLogic; 027import com.echothree.model.control.offer.server.control.OfferControl; 028import com.echothree.model.control.offer.server.logic.OfferLogic; 029import com.echothree.model.control.party.common.PartyTypes; 030import com.echothree.model.control.security.common.SecurityRoleGroups; 031import com.echothree.model.control.security.common.SecurityRoles; 032import com.echothree.model.control.selector.common.SelectorKinds; 033import com.echothree.model.control.selector.common.SelectorTypes; 034import com.echothree.model.control.selector.server.logic.SelectorLogic; 035import com.echothree.model.control.sequence.common.SequenceTypes; 036import com.echothree.model.control.sequence.server.logic.SequenceLogic; 037import com.echothree.model.data.filter.server.entity.Filter; 038import com.echothree.model.data.offer.server.entity.Offer; 039import com.echothree.model.data.selector.server.entity.Selector; 040import com.echothree.model.data.sequence.server.entity.Sequence; 041import com.echothree.util.common.message.ExecutionErrors; 042import com.echothree.util.common.validation.FieldDefinition; 043import com.echothree.util.common.validation.FieldType; 044import com.echothree.util.server.control.BaseAbstractEditCommand; 045import com.echothree.util.server.control.CommandSecurityDefinition; 046import com.echothree.util.server.control.PartyTypeDefinition; 047import com.echothree.util.server.control.SecurityRoleDefinition; 048import java.util.List; 049import javax.enterprise.context.Dependent; 050import javax.inject.Inject; 051 052@Dependent 053public class EditOfferCommand 054 extends BaseAbstractEditCommand<OfferSpec, OfferEdit, EditOfferResult, Offer, Offer> { 055 056 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 057 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 058 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 059 060 static { 061 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 062 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 063 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 064 new SecurityRoleDefinition(SecurityRoleGroups.Offer.name(), SecurityRoles.Edit.name()) 065 )) 066 )); 067 068 SPEC_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, null) 070 ); 071 072 EDIT_FIELD_DEFINITIONS = List.of( 073 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, null), 074 new FieldDefinition("SalesOrderSequenceName", FieldType.ENTITY_NAME, false, null, null), 075 new FieldDefinition("OfferItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("OfferItemPriceFilterName", FieldType.ENTITY_NAME, false, null, null), 077 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 078 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 079 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 080 ); 081 } 082 083 @Inject 084 OfferControl offerControl; 085 086 @Inject 087 FilterLogic filterLogic; 088 089 @Inject 090 OfferLogic offerLogic; 091 092 @Inject 093 SelectorLogic selectorLogic; 094 095 @Inject 096 SequenceLogic sequenceLogic; 097 098 /** Creates a new instance of EditOfferCommand */ 099 public EditOfferCommand() { 100 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 101 } 102 103 @Override 104 public EditOfferResult getResult() { 105 return OfferResultFactory.getEditOfferResult(); 106 } 107 108 @Override 109 public OfferEdit getEdit() { 110 return OfferEditFactory.getOfferEdit(); 111 } 112 113 @Override 114 public Offer getEntity(EditOfferResult result) { 115 return offerLogic.getOfferByName(this, spec.getOfferName(), editModeToEntityPermission(editMode)); 116 } 117 118 @Override 119 public Offer getLockEntity(Offer offer) { 120 return offer; 121 } 122 123 @Override 124 public void fillInResult(EditOfferResult result, Offer offer) { 125 result.setOffer(offerControl.getOfferTransfer(getUserVisit(), offer)); 126 } 127 128 private Sequence salesOrderSequence; 129 private Selector offerItemSelector; 130 private Filter offerItemPriceFilter; 131 132 @Override 133 public void doLock(OfferEdit edit, Offer offer) { 134 var offerDescription = offerControl.getOfferDescription(offer, getPreferredLanguage()); 135 var offerDetail = offer.getLastDetail(); 136 var salesOrderSequence = offerDetail.getSalesOrderSequence(); 137 var offerItemSelector = offerDetail.getOfferItemSelector(); 138 var offerItemPriceFilter = offerDetail.getOfferItemPriceFilter(); 139 140 edit.setOfferName(offerDetail.getOfferName()); 141 edit.setSalesOrderSequenceName(salesOrderSequence == null? null: salesOrderSequence.getLastDetail().getSequenceName()); 142 edit.setOfferItemSelectorName(offerItemSelector == null? null: offerItemSelector.getLastDetail().getSelectorName()); 143 edit.setOfferItemPriceFilterName(offerItemPriceFilter == null? null: offerItemPriceFilter.getLastDetail().getFilterName()); 144 edit.setIsDefault(offerDetail.getIsDefault().toString()); 145 edit.setSortOrder(offerDetail.getSortOrder().toString()); 146 147 if(offerDescription != null) { 148 edit.setDescription(offerDescription.getDescription()); 149 } 150 } 151 152 @Override 153 public void canUpdate(Offer offer) { 154 var offerName = edit.getOfferName(); 155 var duplicateOffer = offerControl.getOfferByName(offerName); 156 157 if(duplicateOffer == null || offer.equals(duplicateOffer)) { 158 var salesOrderSequenceName = edit.getSalesOrderSequenceName(); 159 160 salesOrderSequence = salesOrderSequenceName == null? null: sequenceLogic.getSequenceByName(this, 161 SequenceTypes.SALES_ORDER.name(), salesOrderSequenceName); 162 163 if(!hasExecutionErrors()) { 164 var offerItemSelectorName = edit.getOfferItemSelectorName(); 165 166 offerItemSelector = offerItemSelectorName == null? null: selectorLogic.getSelectorByName(this, 167 SelectorKinds.ITEM.name(), SelectorTypes.OFFER.name(), offerItemSelectorName); 168 169 if(!hasExecutionErrors()) { 170 var offerItemPriceFilterName = edit.getOfferItemPriceFilterName(); 171 172 offerItemPriceFilter = offerItemPriceFilterName == null? null: filterLogic.getFilterByName(this, 173 FilterKinds.PRICE.name(), FilterTypes.OFFER_ITEM_PRICE.name(), offerItemPriceFilterName); 174 } 175 } 176 } else { 177 addExecutionError(ExecutionErrors.DuplicateOfferName.name(), offerName); 178 } 179 } 180 181 @Override 182 public void doUpdate(Offer offer) { 183 var partyPK = getPartyPK(); 184 var offerDetailValue = offerControl.getOfferDetailValueForUpdate(offer); 185 var offerDescription = offerControl.getOfferDescriptionForUpdate(offer, getPreferredLanguage()); 186 var description = edit.getDescription(); 187 188 offerDetailValue.setOfferName(edit.getOfferName()); 189 offerDetailValue.setSalesOrderSequencePK(salesOrderSequence == null? null: salesOrderSequence.getPrimaryKey()); 190 offerDetailValue.setOfferItemSelectorPK(offerItemSelector == null? null: offerItemSelector.getPrimaryKey()); 191 offerDetailValue.setOfferItemPriceFilterPK(offerItemPriceFilter == null? null: offerItemPriceFilter.getPrimaryKey()); 192 offerDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 193 offerDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 194 195 offerLogic.updateOfferFromValue(offerDetailValue, partyPK); 196 197 if(offerDescription == null && description != null) { 198 offerControl.createOfferDescription(offer, getPreferredLanguage(), description, partyPK); 199 } else if(offerDescription != null && description == null) { 200 offerControl.deleteOfferDescription(offerDescription, partyPK); 201 } else if(offerDescription != null && description != null) { 202 var offerDescriptionValue = offerControl.getOfferDescriptionValue(offerDescription); 203 204 offerDescriptionValue.setDescription(description); 205 offerControl.updateOfferDescriptionFromValue(offerDescriptionValue, partyPK); 206 } 207 } 208 209}