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