001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.offer.server.entity.OfferNameElementDescription; 032import com.echothree.model.data.offer.server.entity.OfferNameElementDetail; 033import com.echothree.model.data.offer.server.value.OfferNameElementDescriptionValue; 034import com.echothree.model.data.offer.server.value.OfferNameElementDetailValue; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 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.BaseAbstractEditCommand; 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; 047 048public class EditOfferNameElementCommand 049 extends BaseAbstractEditCommand<OfferNameElementUniversalSpec, OfferNameElementEdit, EditOfferNameElementResult, OfferNameElement, OfferNameElement> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 059 new SecurityRoleDefinition(SecurityRoleGroups.OfferNameElement.name(), SecurityRoles.Edit.name()) 060 ))) 061 ))); 062 063 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 064 new FieldDefinition("OfferNameElementName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 066 new FieldDefinition("Key", FieldType.KEY, false, null, null), 067 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 068 new FieldDefinition("Ulid", FieldType.ULID, false, null, null) 069 )); 070 071 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 072 new FieldDefinition("OfferNameElementName", FieldType.ENTITY_NAME, true, null, null), 073 new FieldDefinition("Offset", FieldType.UNSIGNED_INTEGER, true, null, null), 074 new FieldDefinition("Length", FieldType.UNSIGNED_INTEGER, true, null, null), 075 new FieldDefinition("ValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null), 076 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 077 )); 078 } 079 080 /** Creates a new instance of EditOfferNameElementCommand */ 081 public EditOfferNameElementCommand(UserVisitPK userVisitPK, EditOfferNameElementForm form) { 082 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 083 } 084 085 @Override 086 public EditOfferNameElementResult getResult() { 087 return OfferResultFactory.getEditOfferNameElementResult(); 088 } 089 090 @Override 091 public OfferNameElementEdit getEdit() { 092 return OfferEditFactory.getOfferNameElementEdit(); 093 } 094 095 @Override 096 public OfferNameElement getEntity(EditOfferNameElementResult result) { 097 return OfferNameElementLogic.getInstance().getOfferNameElementByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 098 } 099 100 @Override 101 public OfferNameElement getLockEntity(OfferNameElement offerNameElement) { 102 return offerNameElement; 103 } 104 105 @Override 106 public void fillInResult(EditOfferNameElementResult result, OfferNameElement offerNameElement) { 107 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 108 109 result.setOfferNameElement(offerNameElementControl.getOfferNameElementTransfer(getUserVisit(), offerNameElement)); 110 } 111 112 @Override 113 public void doLock(OfferNameElementEdit edit, OfferNameElement offerNameElement) { 114 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 115 OfferNameElementDescription offerNameElementDescription = offerNameElementControl.getOfferNameElementDescription(offerNameElement, getPreferredLanguage()); 116 OfferNameElementDetail offerNameElementDetail = offerNameElement.getLastDetail(); 117 118 edit.setOfferNameElementName(offerNameElementDetail.getOfferNameElementName()); 119 edit.setOffset(offerNameElementDetail.getOffset().toString()); 120 edit.setLength(offerNameElementDetail.getLength().toString()); 121 edit.setValidationPattern(offerNameElementDetail.getValidationPattern()); 122 123 if(offerNameElementDescription != null) { 124 edit.setDescription(offerNameElementDescription.getDescription()); 125 } 126 } 127 128 @Override 129 public void canUpdate(OfferNameElement offerNameElement) { 130 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 131 String offerNameElementName = edit.getOfferNameElementName(); 132 OfferNameElement duplicateOfferNameElement = offerNameElementControl.getOfferNameElementByName(offerNameElementName); 133 134 if(duplicateOfferNameElement != null && !offerNameElement.equals(duplicateOfferNameElement)) { 135 addExecutionError(ExecutionErrors.DuplicateOfferNameElementName.name(), offerNameElementName); 136 } 137 } 138 139 @Override 140 public void doUpdate(OfferNameElement offerNameElement) { 141 var offerNameElementControl = Session.getModelController(OfferNameElementControl.class); 142 var partyPK = getPartyPK(); 143 OfferNameElementDetailValue offerNameElementDetailValue = offerNameElementControl.getOfferNameElementDetailValueForUpdate(offerNameElement); 144 OfferNameElementDescription offerNameElementDescription = offerNameElementControl.getOfferNameElementDescriptionForUpdate(offerNameElement, getPreferredLanguage()); 145 String description = edit.getDescription(); 146 147 offerNameElementDetailValue.setOfferNameElementName(edit.getOfferNameElementName()); 148 offerNameElementDetailValue.setOffset(Integer.valueOf(edit.getOffset())); 149 offerNameElementDetailValue.setLength(Integer.valueOf(edit.getLength())); 150 offerNameElementDetailValue.setValidationPattern(edit.getValidationPattern()); 151 152 offerNameElementControl.updateOfferNameElementFromValue(offerNameElementDetailValue, partyPK); 153 154 if(offerNameElementDescription == null && description != null) { 155 offerNameElementControl.createOfferNameElementDescription(offerNameElement, getPreferredLanguage(), description, partyPK); 156 } else if(offerNameElementDescription != null && description == null) { 157 offerNameElementControl.deleteOfferNameElementDescription(offerNameElementDescription, partyPK); 158 } else if(offerNameElementDescription != null && description != null) { 159 OfferNameElementDescriptionValue offerNameElementDescriptionValue = offerNameElementControl.getOfferNameElementDescriptionValue(offerNameElementDescription); 160 161 offerNameElementDescriptionValue.setDescription(description); 162 offerNameElementControl.updateOfferNameElementDescriptionFromValue(offerNameElementDescriptionValue, partyPK); 163 } 164 } 165 166}