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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.EntityLongRangeEdit; 021import com.echothree.control.user.core.common.form.EditEntityLongRangeForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditEntityLongRangeResult; 024import com.echothree.control.user.core.common.spec.EntityLongRangeSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.data.core.server.entity.EntityAttribute; 029import com.echothree.model.data.core.server.entity.EntityLongRange; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.EditMode; 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; 041 042@Dependent 043public class EditEntityLongRangeCommand 044 extends BaseAbstractEditCommand<EntityLongRangeSpec, EntityLongRangeEdit, EditEntityLongRangeResult, EntityLongRange, EntityLongRange> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 048 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.EntityLongRange.name(), SecurityRoles.Edit.name()) 055 )) 056 )); 057 058 SPEC_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null), 061 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("EntityLongRangeName", FieldType.ENTITY_NAME, true, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("EntityLongRangeName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("MinimumLongValue", FieldType.SIGNED_LONG, false, null, null), 068 new FieldDefinition("MaximumLongValue", FieldType.SIGNED_LONG, false, null, null), 069 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 070 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 071 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 072 ); 073 } 074 075 /** Creates a new instance of EditEntityLongRangeCommand */ 076 public EditEntityLongRangeCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditEntityLongRangeResult getResult() { 082 return CoreResultFactory.getEditEntityLongRangeResult(); 083 } 084 085 @Override 086 public EntityLongRangeEdit getEdit() { 087 return CoreEditFactory.getEntityLongRangeEdit(); 088 } 089 090 EntityAttribute entityAttribute = null; 091 092 @Override 093 public EntityLongRange getEntity(EditEntityLongRangeResult result) { 094 EntityLongRange entityLongRange = null; 095 var componentVendorName = spec.getComponentVendorName(); 096 var componentVendor = componentControl.getComponentVendorByName(componentVendorName); 097 098 if(componentVendor != null) { 099 var entityTypeName = spec.getEntityTypeName(); 100 var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName); 101 102 if(entityType != null) { 103 var entityAttributeName = spec.getEntityAttributeName(); 104 105 entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName); 106 107 if(entityAttribute != null) { 108 var entityLongRangeName = spec.getEntityLongRangeName(); 109 110 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 111 entityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName); 112 } else { // EditMode.UPDATE 113 entityLongRange = coreControl.getEntityLongRangeByNameForUpdate(entityAttribute, entityLongRangeName); 114 } 115 116 if(entityLongRange == null) { 117 addExecutionError(ExecutionErrors.UnknownEntityLongRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityLongRangeName); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 127 } 128 129 return entityLongRange; 130 } 131 132 @Override 133 public EntityLongRange getLockEntity(EntityLongRange entityLongRange) { 134 return entityLongRange; 135 } 136 137 @Override 138 public void fillInResult(EditEntityLongRangeResult result, EntityLongRange entityLongRange) { 139 140 result.setEntityLongRange(coreControl.getEntityLongRangeTransfer(getUserVisit(), entityLongRange, null)); 141 } 142 143 @Override 144 public void doLock(EntityLongRangeEdit edit, EntityLongRange entityLongRange) { 145 var entityLongRangeDescription = coreControl.getEntityLongRangeDescription(entityLongRange, getPreferredLanguage()); 146 var entityLongRangeDetail = entityLongRange.getLastDetail(); 147 var minimumLongValue = entityLongRangeDetail.getMinimumLongValue(); 148 var maximumLongValue = entityLongRangeDetail.getMaximumLongValue(); 149 150 edit.setEntityLongRangeName(entityLongRangeDetail.getEntityLongRangeName()); 151 edit.setMinimumLongValue(minimumLongValue == null ? null : minimumLongValue.toString()); 152 edit.setMaximumLongValue(maximumLongValue == null ? null : maximumLongValue.toString()); 153 edit.setIsDefault(entityLongRangeDetail.getIsDefault().toString()); 154 edit.setSortOrder(entityLongRangeDetail.getSortOrder().toString()); 155 156 if(entityLongRangeDescription != null) { 157 edit.setDescription(entityLongRangeDescription.getDescription()); 158 } 159 } 160 161 @Override 162 public void canUpdate(EntityLongRange entityLongRange) { 163 var strMinimumLongValue = edit.getMinimumLongValue(); 164 var minimumLongValue = strMinimumLongValue == null ? null : Long.valueOf(strMinimumLongValue); 165 var strMaximumLongValue = edit.getMaximumLongValue(); 166 var maximumLongValue = strMaximumLongValue == null ? null : Long.valueOf(strMaximumLongValue); 167 168 if(minimumLongValue == null || maximumLongValue == null || maximumLongValue >= minimumLongValue) { 169 var entityLongRangeName = edit.getEntityLongRangeName(); 170 var duplicateEntityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName); 171 172 if(duplicateEntityLongRange != null && !entityLongRange.equals(duplicateEntityLongRange)) { 173 addExecutionError(ExecutionErrors.DuplicateEntityLongRangeName.name(), entityLongRangeName); 174 } 175 } else { 176 addExecutionError(ExecutionErrors.MinimumValueGreaterThanMaximumValue.name(), strMinimumLongValue, strMaximumLongValue); 177 } 178 } 179 180 @Override 181 public void doUpdate(EntityLongRange entityLongRange) { 182 var partyPK = getPartyPK(); 183 var entityLongRangeDetailValue = coreControl.getEntityLongRangeDetailValueForUpdate(entityLongRange); 184 var entityLongRangeDescription = coreControl.getEntityLongRangeDescriptionForUpdate(entityLongRange, getPreferredLanguage()); 185 var strMinimumLongValue = edit.getMinimumLongValue(); 186 var strMaximumLongValue = edit.getMaximumLongValue(); 187 var description = edit.getDescription(); 188 189 entityLongRangeDetailValue.setEntityLongRangeName(edit.getEntityLongRangeName()); 190 entityLongRangeDetailValue.setMinimumLongValue(strMinimumLongValue == null ? null : Long.valueOf(strMinimumLongValue)); 191 entityLongRangeDetailValue.setMaximumLongValue(strMaximumLongValue == null ? null : Long.valueOf(strMaximumLongValue)); 192 entityLongRangeDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 193 entityLongRangeDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 194 195 coreControl.updateEntityLongRangeFromValue(entityLongRangeDetailValue, partyPK); 196 197 if(entityLongRangeDescription == null && description != null) { 198 coreControl.createEntityLongRangeDescription(entityLongRange, getPreferredLanguage(), description, partyPK); 199 } else { 200 if(entityLongRangeDescription != null && description == null) { 201 coreControl.deleteEntityLongRangeDescription(entityLongRangeDescription, partyPK); 202 } else { 203 if(entityLongRangeDescription != null && description != null) { 204 var entityLongRangeDescriptionValue = coreControl.getEntityLongRangeDescriptionValue(entityLongRangeDescription); 205 206 entityLongRangeDescriptionValue.setDescription(description); 207 coreControl.updateEntityLongRangeDescriptionFromValue(entityLongRangeDescriptionValue, partyPK); 208 } 209 } 210 } 211 } 212 213}