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.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.Arrays; 041import java.util.Collections; 042import java.util.List; 043import javax.enterprise.context.RequestScoped; 044 045@RequestScoped 046public class EditOfferNameElementCommand 047 extends BaseAbstractEditCommand<OfferNameElementUniversalSpec, OfferNameElementEdit, EditOfferNameElementResult, OfferNameElement, OfferNameElement> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 051 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 057 new SecurityRoleDefinition(SecurityRoleGroups.OfferNameElement.name(), SecurityRoles.Edit.name()) 058 ))) 059 ))); 060 061 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("OfferNameElementName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 064 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("OfferNameElementName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("Offset", FieldType.UNSIGNED_INTEGER, true, null, null), 070 new FieldDefinition("Length", FieldType.UNSIGNED_INTEGER, true, null, null), 071 new FieldDefinition("ValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null), 072 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 073 )); 074 } 075 076 /** Creates a new instance of EditOfferNameElementCommand */ 077 public EditOfferNameElementCommand() { 078 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 public EditOfferNameElementResult getResult() { 083 return OfferResultFactory.getEditOfferNameElementResult(); 084 } 085 086 @Override 087 public OfferNameElementEdit getEdit() { 088 return OfferEditFactory.getOfferNameElementEdit(); 089 } 090 091 @Override 092 public OfferNameElement getEntity(EditOfferNameElementResult result) { 093 return OfferNameElementLogic.getInstance().getOfferNameElementByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 094 } 095 096 @Override 097 public OfferNameElement getLockEntity(OfferNameElement offerNameElement) { 098 return offerNameElement; 099 } 100 101 @Override 102 public void fillInResult(EditOfferNameElementResult result, OfferNameElement offerNameElement) { 103 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 104 105 result.setOfferNameElement(offerNameElementControl.getOfferNameElementTransfer(getUserVisit(), offerNameElement)); 106 } 107 108 @Override 109 public void doLock(OfferNameElementEdit edit, OfferNameElement offerNameElement) { 110 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 111 var offerNameElementDescription = offerNameElementControl.getOfferNameElementDescription(offerNameElement, getPreferredLanguage()); 112 var offerNameElementDetail = offerNameElement.getLastDetail(); 113 114 edit.setOfferNameElementName(offerNameElementDetail.getOfferNameElementName()); 115 edit.setOffset(offerNameElementDetail.getOffset().toString()); 116 edit.setLength(offerNameElementDetail.getLength().toString()); 117 edit.setValidationPattern(offerNameElementDetail.getValidationPattern()); 118 119 if(offerNameElementDescription != null) { 120 edit.setDescription(offerNameElementDescription.getDescription()); 121 } 122 } 123 124 @Override 125 public void canUpdate(OfferNameElement offerNameElement) { 126 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 127 var offerNameElementName = edit.getOfferNameElementName(); 128 var duplicateOfferNameElement = offerNameElementControl.getOfferNameElementByName(offerNameElementName); 129 130 if(duplicateOfferNameElement != null && !offerNameElement.equals(duplicateOfferNameElement)) { 131 addExecutionError(ExecutionErrors.DuplicateOfferNameElementName.name(), offerNameElementName); 132 } 133 } 134 135 @Override 136 public void doUpdate(OfferNameElement offerNameElement) { 137 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 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}