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.EntityAttributeGroupDescriptionEdit;
021import com.echothree.control.user.core.common.result.CoreResultFactory;
022import com.echothree.control.user.core.common.result.EditEntityAttributeGroupDescriptionResult;
023import com.echothree.control.user.core.common.spec.EntityAttributeGroupDescriptionSpec;
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.party.server.control.PartyControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.core.server.entity.EntityAttributeGroup;
031import com.echothree.model.data.core.server.entity.EntityAttributeGroupDescription;
032import com.echothree.util.common.command.EditMode;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditEntityAttributeGroupDescriptionCommand
046        extends BaseAbstractEditCommand<EntityAttributeGroupDescriptionSpec, EntityAttributeGroupDescriptionEdit, EditEntityAttributeGroupDescriptionResult, EntityAttributeGroupDescription, EntityAttributeGroup> {
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(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.EntityAttributeGroup.name(), SecurityRoles.Description.name())
057                ))
058        ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
063                new FieldDefinition("EntityAttributeGroupName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
065        );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
069        );
070    }
071
072    @Inject
073    protected EntityAttributeGroupControl entityAttributeGroupControl;
074
075    @Inject
076    protected EntityAttributeGroupLogic entityAttributeGroupLogic;
077
078    @Inject
079    PartyControl partyControl;
080
081    
082    /** Creates a new instance of EditEntityAttributeGroupDescriptionCommand */
083    public EditEntityAttributeGroupDescriptionCommand() {
084        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
085    }
086
087    @Override
088    public EditEntityAttributeGroupDescriptionResult getResult() {
089        return CoreResultFactory.getEditEntityAttributeGroupDescriptionResult();
090    }
091
092    @Override
093    public EntityAttributeGroupDescriptionEdit getEdit() {
094        return CoreEditFactory.getEntityAttributeGroupDescriptionEdit();
095    }
096
097    @Override
098    public EntityAttributeGroupDescription getEntity(EditEntityAttributeGroupDescriptionResult result) {
099        EntityAttributeGroupDescription entityAttributeGroupDescription = null;
100        var entityAttributeGroupName = spec.getEntityAttributeGroupName();
101        var entityAttributeGroup = entityAttributeGroupLogic.getEntityAttributeGroupByName(this, spec.getComponentVendorName(), spec.getEntityTypeName(), entityAttributeGroupName);
102
103        if(entityAttributeGroup != null) {
104            var languageIsoName = spec.getLanguageIsoName();
105            var language = partyControl.getLanguageByIsoName(languageIsoName);
106
107            if(language != null) {
108                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
109                    entityAttributeGroupDescription = entityAttributeGroupControl.getEntityAttributeGroupDescription(entityAttributeGroup, language);
110                } else { // EditMode.UPDATE
111                    entityAttributeGroupDescription = entityAttributeGroupControl.getEntityAttributeGroupDescriptionForUpdate(entityAttributeGroup, language);
112                }
113
114                if(entityAttributeGroupDescription == null) {
115                    addExecutionError(ExecutionErrors.UnknownEntityAttributeGroupDescription.name(), spec.getComponentVendorName(), spec.getEntityTypeName(), entityAttributeGroupName, languageIsoName);
116                }
117            } else {
118                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
119            }
120        }
121
122        return entityAttributeGroupDescription;
123    }
124
125    @Override
126    public EntityAttributeGroup getLockEntity(EntityAttributeGroupDescription entityAttributeGroupDescription) {
127        return entityAttributeGroupDescription.getEntityAttributeGroup();
128    }
129
130    @Override
131    public void fillInResult(EditEntityAttributeGroupDescriptionResult result, EntityAttributeGroupDescription entityAttributeGroupDescription) {
132
133        result.setEntityAttributeGroupDescription(entityAttributeGroupControl.getEntityAttributeGroupDescriptionTransfer(getUserVisit(), entityAttributeGroupDescription, null));
134    }
135
136    @Override
137    public void doLock(EntityAttributeGroupDescriptionEdit edit, EntityAttributeGroupDescription entityAttributeGroupDescription) {
138        edit.setDescription(entityAttributeGroupDescription.getDescription());
139    }
140
141    @Override
142    public void doUpdate(EntityAttributeGroupDescription entityAttributeGroupDescription) {
143        var entityAttributeGroupDescriptionValue = entityAttributeGroupControl.getEntityAttributeGroupDescriptionValue(entityAttributeGroupDescription);
144        
145        entityAttributeGroupDescriptionValue.setDescription(edit.getDescription());
146        
147        entityAttributeGroupControl.updateEntityAttributeGroupDescriptionFromValue(entityAttributeGroupDescriptionValue, getPartyPK());
148    }
149
150    
151}