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