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