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 com.echothree.util.server.persistence.Session; 040import java.util.List; 041import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditOfferNameElementCommand */ 075 public EditOfferNameElementCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 public EditOfferNameElementResult getResult() { 081 return OfferResultFactory.getEditOfferNameElementResult(); 082 } 083 084 @Override 085 public OfferNameElementEdit getEdit() { 086 return OfferEditFactory.getOfferNameElementEdit(); 087 } 088 089 @Override 090 public OfferNameElement getEntity(EditOfferNameElementResult result) { 091 return OfferNameElementLogic.getInstance().getOfferNameElementByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 092 } 093 094 @Override 095 public OfferNameElement getLockEntity(OfferNameElement offerNameElement) { 096 return offerNameElement; 097 } 098 099 @Override 100 public void fillInResult(EditOfferNameElementResult result, OfferNameElement offerNameElement) { 101 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 102 103 result.setOfferNameElement(offerNameElementControl.getOfferNameElementTransfer(getUserVisit(), offerNameElement)); 104 } 105 106 @Override 107 public void doLock(OfferNameElementEdit edit, OfferNameElement offerNameElement) { 108 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 109 var offerNameElementDescription = offerNameElementControl.getOfferNameElementDescription(offerNameElement, getPreferredLanguage()); 110 var offerNameElementDetail = offerNameElement.getLastDetail(); 111 112 edit.setOfferNameElementName(offerNameElementDetail.getOfferNameElementName()); 113 edit.setOffset(offerNameElementDetail.getOffset().toString()); 114 edit.setLength(offerNameElementDetail.getLength().toString()); 115 edit.setValidationPattern(offerNameElementDetail.getValidationPattern()); 116 117 if(offerNameElementDescription != null) { 118 edit.setDescription(offerNameElementDescription.getDescription()); 119 } 120 } 121 122 @Override 123 public void canUpdate(OfferNameElement offerNameElement) { 124 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 125 var offerNameElementName = edit.getOfferNameElementName(); 126 var duplicateOfferNameElement = offerNameElementControl.getOfferNameElementByName(offerNameElementName); 127 128 if(duplicateOfferNameElement != null && !offerNameElement.equals(duplicateOfferNameElement)) { 129 addExecutionError(ExecutionErrors.DuplicateOfferNameElementName.name(), offerNameElementName); 130 } 131 } 132 133 @Override 134 public void doUpdate(OfferNameElement offerNameElement) { 135 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 136 var partyPK = getPartyPK(); 137 var offerNameElementDetailValue = offerNameElementControl.getOfferNameElementDetailValueForUpdate(offerNameElement); 138 var offerNameElementDescription = offerNameElementControl.getOfferNameElementDescriptionForUpdate(offerNameElement, getPreferredLanguage()); 139 var description = edit.getDescription(); 140 141 offerNameElementDetailValue.setOfferNameElementName(edit.getOfferNameElementName()); 142 offerNameElementDetailValue.setOffset(Integer.valueOf(edit.getOffset())); 143 offerNameElementDetailValue.setLength(Integer.valueOf(edit.getLength())); 144 offerNameElementDetailValue.setValidationPattern(edit.getValidationPattern()); 145 146 offerNameElementControl.updateOfferNameElementFromValue(offerNameElementDetailValue, partyPK); 147 148 if(offerNameElementDescription == null && description != null) { 149 offerNameElementControl.createOfferNameElementDescription(offerNameElement, getPreferredLanguage(), description, partyPK); 150 } else if(offerNameElementDescription != null && description == null) { 151 offerNameElementControl.deleteOfferNameElementDescription(offerNameElementDescription, partyPK); 152 } else if(offerNameElementDescription != null && description != null) { 153 var offerNameElementDescriptionValue = offerNameElementControl.getOfferNameElementDescriptionValue(offerNameElementDescription); 154 155 offerNameElementDescriptionValue.setDescription(description); 156 offerNameElementControl.updateOfferNameElementDescriptionFromValue(offerNameElementDescriptionValue, partyPK); 157 } 158 } 159 160}