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.EntityIntegerRangeEdit; 021import com.echothree.control.user.core.common.form.EditEntityIntegerRangeForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditEntityIntegerRangeResult; 024import com.echothree.control.user.core.common.spec.EntityIntegerRangeSpec; 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.EntityIntegerRange; 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 EditEntityIntegerRangeCommand 044 extends BaseAbstractEditCommand<EntityIntegerRangeSpec, EntityIntegerRangeEdit, EditEntityIntegerRangeResult, EntityIntegerRange, EntityIntegerRange> { 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.EntityIntegerRange.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("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("MinimumIntegerValue", FieldType.SIGNED_INTEGER, false, null, null), 068 new FieldDefinition("MaximumIntegerValue", FieldType.SIGNED_INTEGER, 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 EditEntityIntegerRangeCommand */ 076 public EditEntityIntegerRangeCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditEntityIntegerRangeResult getResult() { 082 return CoreResultFactory.getEditEntityIntegerRangeResult(); 083 } 084 085 @Override 086 public EntityIntegerRangeEdit getEdit() { 087 return CoreEditFactory.getEntityIntegerRangeEdit(); 088 } 089 090 EntityAttribute entityAttribute = null; 091 092 @Override 093 public EntityIntegerRange getEntity(EditEntityIntegerRangeResult result) { 094 EntityIntegerRange entityIntegerRange = 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 entityIntegerRangeName = spec.getEntityIntegerRangeName(); 109 110 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 111 entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName); 112 } else { // EditMode.UPDATE 113 entityIntegerRange = coreControl.getEntityIntegerRangeByNameForUpdate(entityAttribute, entityIntegerRangeName); 114 } 115 116 if(entityIntegerRange == null) { 117 addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName); 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 entityIntegerRange; 130 } 131 132 @Override 133 public EntityIntegerRange getLockEntity(EntityIntegerRange entityIntegerRange) { 134 return entityIntegerRange; 135 } 136 137 @Override 138 public void fillInResult(EditEntityIntegerRangeResult result, EntityIntegerRange entityIntegerRange) { 139 140 result.setEntityIntegerRange(coreControl.getEntityIntegerRangeTransfer(getUserVisit(), entityIntegerRange, null)); 141 } 142 143 @Override 144 public void doLock(EntityIntegerRangeEdit edit, EntityIntegerRange entityIntegerRange) { 145 var entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescription(entityIntegerRange, getPreferredLanguage()); 146 var entityIntegerRangeDetail = entityIntegerRange.getLastDetail(); 147 var minimumIntegerValue = entityIntegerRangeDetail.getMinimumIntegerValue(); 148 var maximumIntegerValue = entityIntegerRangeDetail.getMaximumIntegerValue(); 149 150 edit.setEntityIntegerRangeName(entityIntegerRangeDetail.getEntityIntegerRangeName()); 151 edit.setMinimumIntegerValue(minimumIntegerValue == null ? null : minimumIntegerValue.toString()); 152 edit.setMaximumIntegerValue(maximumIntegerValue == null ? null : maximumIntegerValue.toString()); 153 edit.setIsDefault(entityIntegerRangeDetail.getIsDefault().toString()); 154 edit.setSortOrder(entityIntegerRangeDetail.getSortOrder().toString()); 155 156 if(entityIntegerRangeDescription != null) { 157 edit.setDescription(entityIntegerRangeDescription.getDescription()); 158 } 159 } 160 161 @Override 162 public void canUpdate(EntityIntegerRange entityIntegerRange) { 163 var strMinimumIntegerValue = edit.getMinimumIntegerValue(); 164 var minimumIntegerValue = strMinimumIntegerValue == null ? null : Integer.valueOf(strMinimumIntegerValue); 165 var strMaximumIntegerValue = edit.getMaximumIntegerValue(); 166 var maximumIntegerValue = strMaximumIntegerValue == null ? null : Integer.valueOf(strMaximumIntegerValue); 167 168 if(minimumIntegerValue == null || maximumIntegerValue == null || maximumIntegerValue >= minimumIntegerValue) { 169 var entityIntegerRangeName = edit.getEntityIntegerRangeName(); 170 var duplicateEntityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName); 171 172 if(duplicateEntityIntegerRange != null && !entityIntegerRange.equals(duplicateEntityIntegerRange)) { 173 addExecutionError(ExecutionErrors.DuplicateEntityIntegerRangeName.name(), entityIntegerRangeName); 174 } 175 } else { 176 addExecutionError(ExecutionErrors.MinimumValueGreaterThanMaximumValue.name(), strMinimumIntegerValue, strMaximumIntegerValue); 177 } 178 } 179 180 @Override 181 public void doUpdate(EntityIntegerRange entityIntegerRange) { 182 var partyPK = getPartyPK(); 183 var entityIntegerRangeDetailValue = coreControl.getEntityIntegerRangeDetailValueForUpdate(entityIntegerRange); 184 var entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescriptionForUpdate(entityIntegerRange, getPreferredLanguage()); 185 var strMinimumIntegerValue = edit.getMinimumIntegerValue(); 186 var strMaximumIntegerValue = edit.getMaximumIntegerValue(); 187 var description = edit.getDescription(); 188 189 entityIntegerRangeDetailValue.setEntityIntegerRangeName(edit.getEntityIntegerRangeName()); 190 entityIntegerRangeDetailValue.setMinimumIntegerValue(strMinimumIntegerValue == null ? null : Integer.valueOf(strMinimumIntegerValue)); 191 entityIntegerRangeDetailValue.setMaximumIntegerValue(strMaximumIntegerValue == null ? null : Integer.valueOf(strMaximumIntegerValue)); 192 entityIntegerRangeDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 193 entityIntegerRangeDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 194 195 coreControl.updateEntityIntegerRangeFromValue(entityIntegerRangeDetailValue, partyPK); 196 197 if(entityIntegerRangeDescription == null && description != null) { 198 coreControl.createEntityIntegerRangeDescription(entityIntegerRange, getPreferredLanguage(), description, partyPK); 199 } else { 200 if(entityIntegerRangeDescription != null && description == null) { 201 coreControl.deleteEntityIntegerRangeDescription(entityIntegerRangeDescription, partyPK); 202 } else { 203 if(entityIntegerRangeDescription != null && description != null) { 204 var entityIntegerRangeDescriptionValue = coreControl.getEntityIntegerRangeDescriptionValue(entityIntegerRangeDescription); 205 206 entityIntegerRangeDescriptionValue.setDescription(description); 207 coreControl.updateEntityIntegerRangeDescriptionFromValue(entityIntegerRangeDescriptionValue, partyPK); 208 } 209 } 210 } 211 } 212 213}