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.form.EditOfferForm; 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.control.FilterControl; 027import com.echothree.model.control.filter.server.control.FilterKindControl; 028import com.echothree.model.control.filter.server.control.FilterTypeControl; 029import com.echothree.model.control.offer.server.control.OfferControl; 030import com.echothree.model.control.offer.server.logic.OfferLogic; 031import com.echothree.model.control.party.common.PartyTypes; 032import com.echothree.model.control.security.common.SecurityRoleGroups; 033import com.echothree.model.control.security.common.SecurityRoles; 034import com.echothree.model.control.selector.common.SelectorKinds; 035import com.echothree.model.control.selector.common.SelectorTypes; 036import com.echothree.model.control.selector.server.control.SelectorControl; 037import com.echothree.model.control.sequence.common.SequenceTypes; 038import com.echothree.model.control.sequence.server.control.SequenceControl; 039import com.echothree.model.data.filter.server.entity.Filter; 040import com.echothree.model.data.selector.server.entity.Selector; 041import com.echothree.model.data.sequence.server.entity.Sequence; 042import com.echothree.model.data.user.common.pk.UserVisitPK; 043import com.echothree.util.common.command.BaseResult; 044import com.echothree.util.common.command.EditMode; 045import com.echothree.util.common.message.ExecutionErrors; 046import com.echothree.util.common.validation.FieldDefinition; 047import com.echothree.util.common.validation.FieldType; 048import com.echothree.util.server.control.BaseEditCommand; 049import com.echothree.util.server.control.CommandSecurityDefinition; 050import com.echothree.util.server.control.PartyTypeDefinition; 051import com.echothree.util.server.control.SecurityRoleDefinition; 052import java.util.List; 053import javax.enterprise.context.Dependent; 054import javax.inject.Inject; 055 056@Dependent 057public class EditOfferCommand 058 extends BaseEditCommand<OfferSpec, OfferEdit> { 059 060 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 061 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 062 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 063 064 static { 065 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 066 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 067 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 068 new SecurityRoleDefinition(SecurityRoleGroups.Offer.name(), SecurityRoles.Edit.name()) 069 )) 070 )); 071 072 SPEC_FIELD_DEFINITIONS = List.of( 073 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, null) 074 ); 075 076 EDIT_FIELD_DEFINITIONS = List.of( 077 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, null), 078 new FieldDefinition("SalesOrderSequenceName", FieldType.ENTITY_NAME, false, null, null), 079 new FieldDefinition("OfferItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 080 new FieldDefinition("OfferItemPriceFilterName", FieldType.ENTITY_NAME, false, null, null), 081 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 082 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 083 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 084 ); 085 } 086 087 @Inject 088 FilterControl filterControl; 089 090 @Inject 091 FilterKindControl filterKindControl; 092 093 @Inject 094 FilterTypeControl filterTypeControl; 095 096 @Inject 097 OfferControl offerControl; 098 099 @Inject 100 SelectorControl selectorControl; 101 102 @Inject 103 SequenceControl sequenceControl; 104 105 @Inject 106 OfferLogic offerLogic; 107 108 109 /** Creates a new instance of EditOfferCommand */ 110 public EditOfferCommand() { 111 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 112 } 113 114 @Override 115 protected BaseResult execute() { 116 var result = OfferResultFactory.getEditOfferResult(); 117 118 if(editMode.equals(EditMode.LOCK)) { 119 var offerName = spec.getOfferName(); 120 var offer = offerControl.getOfferByName(offerName); 121 122 if(offer != null) { 123 result.setOffer(offerControl.getOfferTransfer(getUserVisit(), offer)); 124 125 if(lockEntity(offer)) { 126 var offerDescription = offerControl.getOfferDescription(offer, getPreferredLanguage()); 127 var edit = OfferEditFactory.getOfferEdit(); 128 var offerDetail = offer.getLastDetail(); 129 var salesOrderSequence = offerDetail.getSalesOrderSequence(); 130 var offerItemSelector = offerDetail.getOfferItemSelector(); 131 var offerItemPriceFilter = offerDetail.getOfferItemPriceFilter(); 132 133 result.setEdit(edit); 134 edit.setOfferName(offerDetail.getOfferName()); 135 edit.setSalesOrderSequenceName(salesOrderSequence == null? null: salesOrderSequence.getLastDetail().getSequenceName()); 136 edit.setOfferItemSelectorName(offerItemSelector == null? null: offerItemSelector.getLastDetail().getSelectorName()); 137 edit.setOfferItemPriceFilterName(offerItemPriceFilter == null? null: offerItemPriceFilter.getLastDetail().getFilterName()); 138 edit.setIsDefault(offerDetail.getIsDefault().toString()); 139 edit.setSortOrder(offerDetail.getSortOrder().toString()); 140 141 if(offerDescription != null) { 142 edit.setDescription(offerDescription.getDescription()); 143 } 144 } else { 145 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 146 } 147 148 result.setEntityLock(getEntityLockTransfer(offer)); 149 } else { 150 addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName); 151 } 152 } else if(editMode.equals(EditMode.UPDATE)) { 153 var offerName = spec.getOfferName(); 154 var offer = offerControl.getOfferByNameForUpdate(offerName); 155 156 if(offer != null) { 157 offerName = edit.getOfferName(); 158 var duplicateOffer = offerControl.getOfferByName(offerName); 159 160 if(duplicateOffer == null || offer.equals(duplicateOffer)) { 161 var salesOrderSequenceName = edit.getSalesOrderSequenceName(); 162 Sequence salesOrderSequence = null; 163 164 if(salesOrderSequenceName != null) { 165 var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.SALES_ORDER.name()); 166 167 if(sequenceType != null) { 168 salesOrderSequence = sequenceControl.getSequenceByName(sequenceType, salesOrderSequenceName); 169 } else { 170 addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.SALES_ORDER.name()); 171 } 172 } 173 174 if(salesOrderSequenceName == null || salesOrderSequence != null) { 175 var offerItemSelectorName = edit.getOfferItemSelectorName(); 176 Selector offerItemSelector = null; 177 178 if(offerItemSelectorName != null) { 179 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 180 181 if(selectorKind != null) { 182 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 183 SelectorTypes.OFFER.name()); 184 185 if(selectorType != null) { 186 offerItemSelector = selectorControl.getSelectorByName(selectorType, offerItemSelectorName); 187 } else { 188 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.OFFER.name()); 189 } 190 } else { 191 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 192 } 193 } 194 195 if(offerItemSelectorName == null || offerItemSelector != null) { 196 var offerItemPriceFilterName = edit.getOfferItemPriceFilterName(); 197 Filter offerItemPriceFilter = null; 198 199 if(offerItemPriceFilterName != null) { 200 var filterKind = filterKindControl.getFilterKindByName(FilterKinds.PRICE.name()); 201 var filterType = filterTypeControl.getFilterTypeByName(filterKind, FilterTypes.OFFER_ITEM_PRICE.name()); 202 203 if(filterType != null) { 204 offerItemPriceFilter = filterControl.getFilterByName(filterType, offerItemPriceFilterName); 205 } 206 } 207 208 if(offerItemPriceFilterName == null || offerItemPriceFilter != null) { 209 if(lockEntityForUpdate(offer)) { 210 try { 211 var partyPK = getPartyPK(); 212 var offerDetailValue = offerControl.getOfferDetailValueForUpdate(offer); 213 var offerDescription = offerControl.getOfferDescriptionForUpdate(offer, getPreferredLanguage()); 214 var description = edit.getDescription(); 215 216 offerDetailValue.setOfferName(edit.getOfferName()); 217 offerDetailValue.setSalesOrderSequencePK(salesOrderSequence == null? null: salesOrderSequence.getPrimaryKey()); 218 offerDetailValue.setOfferItemSelectorPK(offerItemSelector == null? null: offerItemSelector.getPrimaryKey()); 219 offerDetailValue.setOfferItemPriceFilterPK(offerItemPriceFilter == null? null: offerItemPriceFilter.getPrimaryKey()); 220 offerDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 221 offerDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 222 223 offerLogic.updateOfferFromValue(offerDetailValue, partyPK); 224 225 if(offerDescription == null && description != null) { 226 offerControl.createOfferDescription(offer, getPreferredLanguage(), description, partyPK); 227 } else if(offerDescription != null && description == null) { 228 offerControl.deleteOfferDescription(offerDescription, partyPK); 229 } else if(offerDescription != null && description != null) { 230 var offerDescriptionValue = offerControl.getOfferDescriptionValue(offerDescription); 231 232 offerDescriptionValue.setDescription(description); 233 offerControl.updateOfferDescriptionFromValue(offerDescriptionValue, partyPK); 234 } 235 } finally { 236 unlockEntity(offer); 237 } 238 } else { 239 addExecutionError(ExecutionErrors.EntityLockStale.name()); 240 } 241 } else { 242 addExecutionError(ExecutionErrors.UnknownOfferItemPriceFilterName.name(), offerItemPriceFilterName); 243 } 244 } else { 245 addExecutionError(ExecutionErrors.UnknownOfferItemSelectorName.name(), offerItemSelectorName); 246 } 247 } else { 248 addExecutionError(ExecutionErrors.UnknownSalesOrderSequenceName.name(), salesOrderSequenceName); 249 } 250 } else { 251 addExecutionError(ExecutionErrors.DuplicateOfferName.name(), offerName); 252 } 253 } else { 254 addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName); 255 } 256 } 257 258 return result; 259 } 260 261}