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