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.OfferNameElementEdit; 021import com.echothree.control.user.offer.common.form.EditOfferNameElementForm; 022import com.echothree.control.user.offer.common.result.EditOfferNameElementResult; 023import com.echothree.control.user.offer.common.result.OfferResultFactory; 024import com.echothree.control.user.offer.common.spec.OfferNameElementUniversalSpec; 025import com.echothree.model.control.offer.server.control.OfferNameElementControl; 026import com.echothree.model.control.offer.server.logic.OfferNameElementLogic; 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.data.offer.server.entity.OfferNameElement; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 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 EditOfferNameElementCommand 045 extends BaseAbstractEditCommand<OfferNameElementUniversalSpec, OfferNameElementEdit, EditOfferNameElementResult, OfferNameElement, OfferNameElement> { 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.OfferNameElement.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("OfferNameElementName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 062 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("OfferNameElementName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("Offset", FieldType.UNSIGNED_INTEGER, true, null, null), 068 new FieldDefinition("Length", FieldType.UNSIGNED_INTEGER, true, null, null), 069 new FieldDefinition("ValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null), 070 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 071 ); 072 } 073 074 @Inject 075 OfferNameElementControl offerNameElementControl; 076 077 @Inject 078 OfferNameElementLogic offerNameElementLogic; 079 080 081 /** Creates a new instance of EditOfferNameElementCommand */ 082 public EditOfferNameElementCommand() { 083 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 084 } 085 086 @Override 087 public EditOfferNameElementResult getResult() { 088 return OfferResultFactory.getEditOfferNameElementResult(); 089 } 090 091 @Override 092 public OfferNameElementEdit getEdit() { 093 return OfferEditFactory.getOfferNameElementEdit(); 094 } 095 096 @Override 097 public OfferNameElement getEntity(EditOfferNameElementResult result) { 098 return offerNameElementLogic.getOfferNameElementByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 099 } 100 101 @Override 102 public OfferNameElement getLockEntity(OfferNameElement offerNameElement) { 103 return offerNameElement; 104 } 105 106 @Override 107 public void fillInResult(EditOfferNameElementResult result, OfferNameElement offerNameElement) { 108 result.setOfferNameElement(offerNameElementControl.getOfferNameElementTransfer(getUserVisit(), offerNameElement)); 109 } 110 111 @Override 112 public void doLock(OfferNameElementEdit edit, OfferNameElement offerNameElement) { 113 var offerNameElementDescription = offerNameElementControl.getOfferNameElementDescription(offerNameElement, getPreferredLanguage()); 114 var offerNameElementDetail = offerNameElement.getLastDetail(); 115 116 edit.setOfferNameElementName(offerNameElementDetail.getOfferNameElementName()); 117 edit.setOffset(offerNameElementDetail.getOffset().toString()); 118 edit.setLength(offerNameElementDetail.getLength().toString()); 119 edit.setValidationPattern(offerNameElementDetail.getValidationPattern()); 120 121 if(offerNameElementDescription != null) { 122 edit.setDescription(offerNameElementDescription.getDescription()); 123 } 124 } 125 126 @Override 127 public void canUpdate(OfferNameElement offerNameElement) { 128 var offerNameElementName = edit.getOfferNameElementName(); 129 var duplicateOfferNameElement = offerNameElementControl.getOfferNameElementByName(offerNameElementName); 130 131 if(duplicateOfferNameElement != null && !offerNameElement.equals(duplicateOfferNameElement)) { 132 addExecutionError(ExecutionErrors.DuplicateOfferNameElementName.name(), offerNameElementName); 133 } 134 } 135 136 @Override 137 public void doUpdate(OfferNameElement offerNameElement) { 138 var partyPK = getPartyPK(); 139 var offerNameElementDetailValue = offerNameElementControl.getOfferNameElementDetailValueForUpdate(offerNameElement); 140 var offerNameElementDescription = offerNameElementControl.getOfferNameElementDescriptionForUpdate(offerNameElement, getPreferredLanguage()); 141 var description = edit.getDescription(); 142 143 offerNameElementDetailValue.setOfferNameElementName(edit.getOfferNameElementName()); 144 offerNameElementDetailValue.setOffset(Integer.valueOf(edit.getOffset())); 145 offerNameElementDetailValue.setLength(Integer.valueOf(edit.getLength())); 146 offerNameElementDetailValue.setValidationPattern(edit.getValidationPattern()); 147 148 offerNameElementControl.updateOfferNameElementFromValue(offerNameElementDetailValue, partyPK); 149 150 if(offerNameElementDescription == null && description != null) { 151 offerNameElementControl.createOfferNameElementDescription(offerNameElement, getPreferredLanguage(), description, partyPK); 152 } else if(offerNameElementDescription != null && description == null) { 153 offerNameElementControl.deleteOfferNameElementDescription(offerNameElementDescription, partyPK); 154 } else if(offerNameElementDescription != null && description != null) { 155 var offerNameElementDescriptionValue = offerNameElementControl.getOfferNameElementDescriptionValue(offerNameElementDescription); 156 157 offerNameElementDescriptionValue.setDescription(description); 158 offerNameElementControl.updateOfferNameElementDescriptionFromValue(offerNameElementDescriptionValue, partyPK); 159 } 160 } 161 162}