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.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.Arrays; 040import java.util.Collections; 041import java.util.List; 042import javax.enterprise.context.RequestScoped; 043 044@RequestScoped 045public class EditEntityIntegerRangeCommand 046 extends BaseAbstractEditCommand<EntityIntegerRangeSpec, EntityIntegerRangeEdit, EditEntityIntegerRangeResult, EntityIntegerRange, EntityIntegerRange> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 056 new SecurityRoleDefinition(SecurityRoleGroups.EntityIntegerRange.name(), SecurityRoles.Edit.name()) 057 ))) 058 ))); 059 060 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 061 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null), 063 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("MinimumIntegerValue", FieldType.SIGNED_INTEGER, false, null, null), 070 new FieldDefinition("MaximumIntegerValue", FieldType.SIGNED_INTEGER, false, null, null), 071 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 072 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 073 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 074 )); 075 } 076 077 /** Creates a new instance of EditEntityIntegerRangeCommand */ 078 public EditEntityIntegerRangeCommand() { 079 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 080 } 081 082 @Override 083 public EditEntityIntegerRangeResult getResult() { 084 return CoreResultFactory.getEditEntityIntegerRangeResult(); 085 } 086 087 @Override 088 public EntityIntegerRangeEdit getEdit() { 089 return CoreEditFactory.getEntityIntegerRangeEdit(); 090 } 091 092 EntityAttribute entityAttribute = null; 093 094 @Override 095 public EntityIntegerRange getEntity(EditEntityIntegerRangeResult result) { 096 EntityIntegerRange entityIntegerRange = null; 097 var componentVendorName = spec.getComponentVendorName(); 098 var componentVendor = componentControl.getComponentVendorByName(componentVendorName); 099 100 if(componentVendor != null) { 101 var entityTypeName = spec.getEntityTypeName(); 102 var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName); 103 104 if(entityType != null) { 105 var entityAttributeName = spec.getEntityAttributeName(); 106 107 entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName); 108 109 if(entityAttribute != null) { 110 var entityIntegerRangeName = spec.getEntityIntegerRangeName(); 111 112 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 113 entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName); 114 } else { // EditMode.UPDATE 115 entityIntegerRange = coreControl.getEntityIntegerRangeByNameForUpdate(entityAttribute, entityIntegerRangeName); 116 } 117 118 if(entityIntegerRange == null) { 119 addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName); 126 } 127 } else { 128 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 129 } 130 131 return entityIntegerRange; 132 } 133 134 @Override 135 public EntityIntegerRange getLockEntity(EntityIntegerRange entityIntegerRange) { 136 return entityIntegerRange; 137 } 138 139 @Override 140 public void fillInResult(EditEntityIntegerRangeResult result, EntityIntegerRange entityIntegerRange) { 141 142 result.setEntityIntegerRange(coreControl.getEntityIntegerRangeTransfer(getUserVisit(), entityIntegerRange, null)); 143 } 144 145 @Override 146 public void doLock(EntityIntegerRangeEdit edit, EntityIntegerRange entityIntegerRange) { 147 var entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescription(entityIntegerRange, getPreferredLanguage()); 148 var entityIntegerRangeDetail = entityIntegerRange.getLastDetail(); 149 var minimumIntegerValue = entityIntegerRangeDetail.getMinimumIntegerValue(); 150 var maximumIntegerValue = entityIntegerRangeDetail.getMaximumIntegerValue(); 151 152 edit.setEntityIntegerRangeName(entityIntegerRangeDetail.getEntityIntegerRangeName()); 153 edit.setMinimumIntegerValue(minimumIntegerValue == null ? null : minimumIntegerValue.toString()); 154 edit.setMaximumIntegerValue(maximumIntegerValue == null ? null : maximumIntegerValue.toString()); 155 edit.setIsDefault(entityIntegerRangeDetail.getIsDefault().toString()); 156 edit.setSortOrder(entityIntegerRangeDetail.getSortOrder().toString()); 157 158 if(entityIntegerRangeDescription != null) { 159 edit.setDescription(entityIntegerRangeDescription.getDescription()); 160 } 161 } 162 163 @Override 164 public void canUpdate(EntityIntegerRange entityIntegerRange) { 165 var strMinimumIntegerValue = edit.getMinimumIntegerValue(); 166 var minimumIntegerValue = strMinimumIntegerValue == null ? null : Integer.valueOf(strMinimumIntegerValue); 167 var strMaximumIntegerValue = edit.getMaximumIntegerValue(); 168 var maximumIntegerValue = strMaximumIntegerValue == null ? null : Integer.valueOf(strMaximumIntegerValue); 169 170 if(minimumIntegerValue == null || maximumIntegerValue == null || maximumIntegerValue >= minimumIntegerValue) { 171 var entityIntegerRangeName = edit.getEntityIntegerRangeName(); 172 var duplicateEntityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName); 173 174 if(duplicateEntityIntegerRange != null && !entityIntegerRange.equals(duplicateEntityIntegerRange)) { 175 addExecutionError(ExecutionErrors.DuplicateEntityIntegerRangeName.name(), entityIntegerRangeName); 176 } 177 } else { 178 addExecutionError(ExecutionErrors.MinimumValueGreaterThanMaximumValue.name(), strMinimumIntegerValue, strMaximumIntegerValue); 179 } 180 } 181 182 @Override 183 public void doUpdate(EntityIntegerRange entityIntegerRange) { 184 var partyPK = getPartyPK(); 185 var entityIntegerRangeDetailValue = coreControl.getEntityIntegerRangeDetailValueForUpdate(entityIntegerRange); 186 var entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescriptionForUpdate(entityIntegerRange, getPreferredLanguage()); 187 var strMinimumIntegerValue = edit.getMinimumIntegerValue(); 188 var strMaximumIntegerValue = edit.getMaximumIntegerValue(); 189 var description = edit.getDescription(); 190 191 entityIntegerRangeDetailValue.setEntityIntegerRangeName(edit.getEntityIntegerRangeName()); 192 entityIntegerRangeDetailValue.setMinimumIntegerValue(strMinimumIntegerValue == null ? null : Integer.valueOf(strMinimumIntegerValue)); 193 entityIntegerRangeDetailValue.setMaximumIntegerValue(strMaximumIntegerValue == null ? null : Integer.valueOf(strMaximumIntegerValue)); 194 entityIntegerRangeDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 195 entityIntegerRangeDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 196 197 coreControl.updateEntityIntegerRangeFromValue(entityIntegerRangeDetailValue, partyPK); 198 199 if(entityIntegerRangeDescription == null && description != null) { 200 coreControl.createEntityIntegerRangeDescription(entityIntegerRange, getPreferredLanguage(), description, partyPK); 201 } else { 202 if(entityIntegerRangeDescription != null && description == null) { 203 coreControl.deleteEntityIntegerRangeDescription(entityIntegerRangeDescription, partyPK); 204 } else { 205 if(entityIntegerRangeDescription != null && description != null) { 206 var entityIntegerRangeDescriptionValue = coreControl.getEntityIntegerRangeDescriptionValue(entityIntegerRangeDescription); 207 208 entityIntegerRangeDescriptionValue.setDescription(description); 209 coreControl.updateEntityIntegerRangeDescriptionFromValue(entityIntegerRangeDescriptionValue, partyPK); 210 } 211 } 212 } 213 } 214 215}