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.OfferEditFactory; 020import com.echothree.control.user.offer.common.edit.OfferUseEdit; 021import com.echothree.control.user.offer.common.form.EditOfferUseForm; 022import com.echothree.control.user.offer.common.result.OfferResultFactory; 023import com.echothree.control.user.offer.common.spec.OfferUseSpec; 024import com.echothree.model.control.offer.server.control.OfferControl; 025import com.echothree.model.control.offer.server.control.OfferUseControl; 026import com.echothree.model.control.offer.server.control.UseControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.control.sequence.common.SequenceTypes; 031import com.echothree.model.control.sequence.server.control.SequenceControl; 032import com.echothree.model.data.sequence.server.entity.Sequence; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.server.control.BaseEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import com.echothree.util.server.persistence.Session; 044import java.util.List; 045import javax.enterprise.context.Dependent; 046 047@Dependent 048public class EditOfferUseCommand 049 extends BaseEditCommand<OfferUseSpec, OfferUseEdit> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 059 new SecurityRoleDefinition(SecurityRoleGroups.OfferUse.name(), SecurityRoles.Edit.name()) 060 )) 061 )); 062 063 SPEC_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("SalesOrderSequenceName", FieldType.ENTITY_NAME, false, null, null) 070 ); 071 } 072 073 /** Creates a new instance of EditOfferUseCommand */ 074 public EditOfferUseCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 var offerControl = Session.getModelController(OfferControl.class); 081 var result = OfferResultFactory.getEditOfferUseResult(); 082 var offerName = spec.getOfferName(); 083 var offer = offerControl.getOfferByName(offerName); 084 085 if(offer != null) { 086 var useControl = Session.getModelController(UseControl.class); 087 var useName = spec.getUseName(); 088 var use = useControl.getUseByName(useName); 089 090 if(use != null) { 091 var offerUseControl = Session.getModelController(OfferUseControl.class); 092 093 if(editMode.equals(EditMode.LOCK)) { 094 var offerUse = offerUseControl.getOfferUse(offer, use); 095 096 if(offerUse != null) { 097 result.setOfferUse(offerUseControl.getOfferUseTransfer(getUserVisit(), offerUse)); 098 099 if(lockEntity(offerUse)) { 100 var edit = OfferEditFactory.getOfferUseEdit(); 101 var offerUseDetail = offerUse.getLastDetail(); 102 var salesOrderSequence = offerUseDetail.getSalesOrderSequence(); 103 104 result.setEdit(edit); 105 edit.setSalesOrderSequenceName(salesOrderSequence == null? null: salesOrderSequence.getLastDetail().getSequenceName()); 106 } else { 107 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 108 } 109 110 result.setEntityLock(getEntityLockTransfer(offerUse)); 111 } else { 112 addExecutionError(ExecutionErrors.UnknownOfferUse.name()); 113 } 114 } else if(editMode.equals(EditMode.UPDATE)) { 115 var offerUse = offerUseControl.getOfferUseForUpdate(offer, use); 116 117 if(offerUse != null) { 118 var salesOrderSequenceName = edit.getSalesOrderSequenceName(); 119 Sequence salesOrderSequence = null; 120 121 if(salesOrderSequenceName != null) { 122 var sequenceControl = Session.getModelController(SequenceControl.class); 123 var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.SALES_ORDER.name()); 124 125 if(sequenceType != null) { 126 salesOrderSequence = sequenceControl.getSequenceByName(sequenceType, salesOrderSequenceName); 127 } else { 128 addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.SALES_ORDER.name()); 129 } 130 } 131 132 if(salesOrderSequenceName == null || salesOrderSequence != null) { 133 if(lockEntityForUpdate(offerUse)) { 134 try { 135 var offerUseDetailValue = offerUseControl.getOfferUseDetailValueForUpdate(offerUse); 136 137 offerUseDetailValue.setSalesOrderSequencePK(salesOrderSequence == null? null: salesOrderSequence.getPrimaryKey()); 138 139 offerUseControl.updateOfferUseFromValue(offerUseDetailValue, getPartyPK()); 140 } finally { 141 unlockEntity(offerUse); 142 } 143 } else { 144 addExecutionError(ExecutionErrors.EntityLockStale.name()); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.UnknownSalesOrderSequenceName.name(), salesOrderSequenceName); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.UnknownOfferUse.name()); 151 } 152 } 153 } else { 154 addExecutionError(ExecutionErrors.UnknownUseName.name(), useName); 155 } 156 } else { 157 addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName); 158 } 159 160 return result; 161 } 162 163}