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.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.List;
039import javax.enterprise.context.Dependent;
040
041@Dependent
042public class EditEntityAttributeGroupCommand
043        extends BaseAbstractEditCommand<EntityAttributeGroupSpec, 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("EntityAttributeGroupName", FieldType.ENTITY_NAME, true, null, null)
059                );
060        
061        EDIT_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("EntityAttributeGroupName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
064                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
065                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
066                );
067    }
068    
069    /** Creates a new instance of EditEntityAttributeGroupCommand */
070    public EditEntityAttributeGroupCommand() {
071        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
072    }
073    
074    @Override
075    public EditEntityAttributeGroupResult getResult() {
076        return CoreResultFactory.getEditEntityAttributeGroupResult();
077    }
078    
079    @Override
080    public EntityAttributeGroupEdit getEdit() {
081        return CoreEditFactory.getEntityAttributeGroupEdit();
082    }
083    
084    @Override
085    public EntityAttributeGroup getEntity(EditEntityAttributeGroupResult result) {
086        EntityAttributeGroup entityAttributeGroup;
087        var entityAttributeGroupName = spec.getEntityAttributeGroupName();
088
089        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
090            entityAttributeGroup = coreControl.getEntityAttributeGroupByName(entityAttributeGroupName);
091
092            if(entityAttributeGroup == null) {
093                addExecutionError(ExecutionErrors.UnknownEntityAttributeGroupName.name(), entityAttributeGroupName);
094            }
095        } else { // EditMode.UPDATE
096            entityAttributeGroup = coreControl.getEntityAttributeGroupByNameForUpdate(entityAttributeGroupName);
097        }
098
099        return entityAttributeGroup;
100    }
101    
102    @Override
103    public EntityAttributeGroup getLockEntity(EntityAttributeGroup entityAttributeGroup) {
104        return entityAttributeGroup;
105    }
106    
107    @Override
108    public void fillInResult(EditEntityAttributeGroupResult result, EntityAttributeGroup entityAttributeGroup) {
109        
110        result.setEntityAttributeGroup(coreControl.getEntityAttributeGroupTransfer(getUserVisit(), entityAttributeGroup, null));
111    }
112    
113    @Override
114    public void doLock(EntityAttributeGroupEdit edit, EntityAttributeGroup entityAttributeGroup) {
115        var entityAttributeGroupDescription = coreControl.getEntityAttributeGroupDescription(entityAttributeGroup, getPreferredLanguage());
116        var entityAttributeGroupDetail = entityAttributeGroup.getLastDetail();
117
118        edit.setEntityAttributeGroupName(entityAttributeGroupDetail.getEntityAttributeGroupName());
119        edit.setIsDefault(entityAttributeGroupDetail.getIsDefault().toString());
120        edit.setSortOrder(entityAttributeGroupDetail.getSortOrder().toString());
121
122        if(entityAttributeGroupDescription != null) {
123            edit.setDescription(entityAttributeGroupDescription.getDescription());
124        }
125    }
126        
127    @Override
128    public void canUpdate(EntityAttributeGroup entityAttributeGroup) {
129        var entityAttributeGroupName = edit.getEntityAttributeGroupName();
130        var duplicateEntityAttributeGroup = coreControl.getEntityAttributeGroupByName(entityAttributeGroupName);
131
132        if(duplicateEntityAttributeGroup != null && !entityAttributeGroup.equals(duplicateEntityAttributeGroup)) {
133            addExecutionError(ExecutionErrors.DuplicateEntityAttributeGroupName.name(), entityAttributeGroupName);
134        }
135    }
136    
137    @Override
138    public void doUpdate(EntityAttributeGroup entityAttributeGroup) {
139        var partyPK = getPartyPK();
140        var entityAttributeGroupDetailValue = coreControl.getEntityAttributeGroupDetailValueForUpdate(entityAttributeGroup);
141        var entityAttributeGroupDescription = coreControl.getEntityAttributeGroupDescriptionForUpdate(entityAttributeGroup, getPreferredLanguage());
142        var description = edit.getDescription();
143
144        entityAttributeGroupDetailValue.setEntityAttributeGroupName(edit.getEntityAttributeGroupName());
145        entityAttributeGroupDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
146        entityAttributeGroupDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
147
148        coreControl.updateEntityAttributeGroupFromValue(entityAttributeGroupDetailValue, partyPK);
149
150        if(entityAttributeGroupDescription == null && description != null) {
151            coreControl.createEntityAttributeGroupDescription(entityAttributeGroup, getPreferredLanguage(), description, partyPK);
152        } else if(entityAttributeGroupDescription != null && description == null) {
153            coreControl.deleteEntityAttributeGroupDescription(entityAttributeGroupDescription, partyPK);
154        } else if(entityAttributeGroupDescription != null && description != null) {
155            var entityAttributeGroupDescriptionValue = coreControl.getEntityAttributeGroupDescriptionValue(entityAttributeGroupDescription);
156
157            entityAttributeGroupDescriptionValue.setDescription(description);
158            coreControl.updateEntityAttributeGroupDescriptionFromValue(entityAttributeGroupDescriptionValue, partyPK);
159        }
160    }
161    
162}