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.result.EditOfferUseResult; 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.OfferUseControl; 025import com.echothree.model.control.offer.server.logic.OfferUseLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.control.sequence.common.SequenceTypes; 030import com.echothree.model.control.sequence.server.logic.SequenceLogic; 031import com.echothree.model.data.offer.server.entity.OfferUse; 032import com.echothree.model.data.sequence.server.entity.Sequence; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseAbstractEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class EditOfferUseCommand 045 extends BaseAbstractEditCommand<OfferUseSpec, OfferUseEdit, EditOfferUseResult, OfferUse, OfferUse> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.OfferUse.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null) 062 ); 063 064 EDIT_FIELD_DEFINITIONS = List.of( 065 new FieldDefinition("SalesOrderSequenceName", FieldType.ENTITY_NAME, false, null, null) 066 ); 067 } 068 069 @Inject 070 OfferUseControl offerUseControl; 071 072 @Inject 073 OfferUseLogic offerUseLogic; 074 075 @Inject 076 SequenceLogic sequenceLogic; 077 078 /** Creates a new instance of EditOfferUseCommand */ 079 public EditOfferUseCommand() { 080 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 081 } 082 083 @Override 084 public EditOfferUseResult getResult() { 085 return OfferResultFactory.getEditOfferUseResult(); 086 } 087 088 @Override 089 public OfferUseEdit getEdit() { 090 return OfferEditFactory.getOfferUseEdit(); 091 } 092 093 @Override 094 public OfferUse getEntity(EditOfferUseResult result) { 095 return offerUseLogic.getOfferUseByName(this, spec.getOfferName(), spec.getUseName(), editModeToEntityPermission(editMode)); 096 } 097 098 @Override 099 public OfferUse getLockEntity(OfferUse offerUse) { 100 return offerUse; 101 } 102 103 @Override 104 public void fillInResult(EditOfferUseResult result, OfferUse offerUse) { 105 result.setOfferUse(offerUseControl.getOfferUseTransfer(getUserVisit(), offerUse)); 106 } 107 108 private Sequence salesOrderSequence; 109 110 @Override 111 public void doLock(OfferUseEdit edit, OfferUse offerUse) { 112 var salesOrderSequence = offerUse.getLastDetail().getSalesOrderSequence(); 113 114 edit.setSalesOrderSequenceName(salesOrderSequence == null? null: salesOrderSequence.getLastDetail().getSequenceName()); 115 } 116 117 @Override 118 public void canUpdate(OfferUse offerUse) { 119 var salesOrderSequenceName = edit.getSalesOrderSequenceName(); 120 121 salesOrderSequence = salesOrderSequenceName == null? null: sequenceLogic.getSequenceByName(this, 122 SequenceTypes.SALES_ORDER.name(), salesOrderSequenceName); 123 } 124 125 @Override 126 public void doUpdate(OfferUse offerUse) { 127 var offerUseDetailValue = offerUseControl.getOfferUseDetailValueForUpdate(offerUse); 128 129 offerUseDetailValue.setSalesOrderSequencePK(salesOrderSequence == null? null: salesOrderSequence.getPrimaryKey()); 130 131 offerUseControl.updateOfferUseFromValue(offerUseDetailValue, getPartyPK()); 132 } 133 134}