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.EntityAttributeGroupEdit; 021import com.echothree.control.user.core.common.result.CoreResultFactory; 022import com.echothree.control.user.core.common.result.EditEntityAttributeGroupResult; 023import com.echothree.control.user.core.common.spec.EntityAttributeGroupUniversalSpec; 024import com.echothree.model.control.core.server.control.EntityAttributeGroupControl; 025import com.echothree.model.control.core.server.logic.EntityAttributeGroupLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.core.server.entity.EntityAttributeGroup; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.server.control.BaseAbstractEditCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class EditEntityAttributeGroupCommand 043 extends BaseAbstractEditCommand<EntityAttributeGroupUniversalSpec, EntityAttributeGroupEdit, EditEntityAttributeGroupResult, EntityAttributeGroup, EntityAttributeGroup> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 051 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 052 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 053 new SecurityRoleDefinition(SecurityRoleGroups.EntityAttributeGroup.name(), SecurityRoles.Edit.name()) 054 )) 055 )); 056 057 SPEC_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, false, null, null), 060 new FieldDefinition("EntityAttributeGroupName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 062 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("EntityAttributeGroupName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 068 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 069 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 070 ); 071 } 072 073 @Inject 074 protected EntityAttributeGroupControl entityAttributeGroupControl; 075 076 @Inject 077 protected EntityAttributeGroupLogic entityAttributeGroupLogic; 078 079 /** Creates a new instance of EditEntityAttributeGroupCommand */ 080 public EditEntityAttributeGroupCommand() { 081 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 082 } 083 084 @Override 085 public EditEntityAttributeGroupResult getResult() { 086 return CoreResultFactory.getEditEntityAttributeGroupResult(); 087 } 088 089 @Override 090 public EntityAttributeGroupEdit getEdit() { 091 return CoreEditFactory.getEntityAttributeGroupEdit(); 092 } 093 094 @Override 095 public EntityAttributeGroup getEntity(EditEntityAttributeGroupResult result) { 096 return entityAttributeGroupLogic.getEntityAttributeGroupByUniversalSpec(this, spec, editModeToEntityPermission(editMode)); 097 } 098 099 @Override 100 public EntityAttributeGroup getLockEntity(EntityAttributeGroup entityAttributeGroup) { 101 return entityAttributeGroup; 102 } 103 104 @Override 105 public void fillInResult(EditEntityAttributeGroupResult result, EntityAttributeGroup entityAttributeGroup) { 106 107 result.setEntityAttributeGroup(entityAttributeGroupControl.getEntityAttributeGroupTransfer(getUserVisit(), entityAttributeGroup, null)); 108 } 109 110 @Override 111 public void doLock(EntityAttributeGroupEdit edit, EntityAttributeGroup entityAttributeGroup) { 112 var entityAttributeGroupDescription = entityAttributeGroupControl.getEntityAttributeGroupDescription(entityAttributeGroup, getPreferredLanguage()); 113 var entityAttributeGroupDetail = entityAttributeGroup.getLastDetail(); 114 115 edit.setEntityAttributeGroupName(entityAttributeGroupDetail.getEntityAttributeGroupName()); 116 edit.setIsDefault(entityAttributeGroupDetail.getIsDefault().toString()); 117 edit.setSortOrder(entityAttributeGroupDetail.getSortOrder().toString()); 118 119 if(entityAttributeGroupDescription != null) { 120 edit.setDescription(entityAttributeGroupDescription.getDescription()); 121 } 122 } 123 124 @Override 125 public void canUpdate(EntityAttributeGroup entityAttributeGroup) { 126 var entityAttributeGroupName = edit.getEntityAttributeGroupName(); 127 var duplicateEntityAttributeGroup = entityAttributeGroupControl.getEntityAttributeGroupByName(entityAttributeGroup.getLastDetail().getEntityType(), entityAttributeGroupName); 128 129 if(duplicateEntityAttributeGroup != null && !entityAttributeGroup.equals(duplicateEntityAttributeGroup)) { 130 var entityTypeDetail = entityAttributeGroup.getLastDetail().getEntityType().getLastDetail(); 131 132 addExecutionError(ExecutionErrors.DuplicateEntityAttributeGroupName.name(), 133 entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 134 entityTypeDetail.getEntityTypeName(), entityAttributeGroupName); 135 } 136 } 137 138 @Override 139 public void doUpdate(EntityAttributeGroup entityAttributeGroup) { 140 var partyPK = getPartyPK(); 141 var entityAttributeGroupDetailValue = entityAttributeGroupControl.getEntityAttributeGroupDetailValueForUpdate(entityAttributeGroup); 142 var entityAttributeGroupDescription = entityAttributeGroupControl.getEntityAttributeGroupDescriptionForUpdate(entityAttributeGroup, getPreferredLanguage()); 143 var description = edit.getDescription(); 144 145 entityAttributeGroupDetailValue.setEntityAttributeGroupName(edit.getEntityAttributeGroupName()); 146 entityAttributeGroupDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 147 entityAttributeGroupDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 148 149 entityAttributeGroupControl.updateEntityAttributeGroupFromValue(entityAttributeGroupDetailValue, partyPK); 150 151 if(entityAttributeGroupDescription == null && description != null) { 152 entityAttributeGroupControl.createEntityAttributeGroupDescription(entityAttributeGroup, getPreferredLanguage(), description, partyPK); 153 } else if(entityAttributeGroupDescription != null && description == null) { 154 entityAttributeGroupControl.deleteEntityAttributeGroupDescription(entityAttributeGroupDescription, partyPK); 155 } else if(entityAttributeGroupDescription != null && description != null) { 156 var entityAttributeGroupDescriptionValue = entityAttributeGroupControl.getEntityAttributeGroupDescriptionValue(entityAttributeGroupDescription); 157 158 entityAttributeGroupDescriptionValue.setDescription(description); 159 entityAttributeGroupControl.updateEntityAttributeGroupDescriptionFromValue(entityAttributeGroupDescriptionValue, partyPK); 160 } 161 } 162 163}