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.form.EditEntityAttributeGroupDescriptionForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityAttributeGroupDescriptionResult;
024import com.echothree.control.user.core.common.spec.EntityAttributeGroupDescriptionSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
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.model.data.core.server.entity.EntityAttributeGroupDescription;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.EditMode;
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("EntityAttributeGroupName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
063        );
064        
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
067        );
068    }
069
070    @Inject
071    PartyControl partyControl;
072
073    
074    /** Creates a new instance of EditEntityAttributeGroupDescriptionCommand */
075    public EditEntityAttributeGroupDescriptionCommand() {
076        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078
079    @Override
080    public EditEntityAttributeGroupDescriptionResult getResult() {
081        return CoreResultFactory.getEditEntityAttributeGroupDescriptionResult();
082    }
083
084    @Override
085    public EntityAttributeGroupDescriptionEdit getEdit() {
086        return CoreEditFactory.getEntityAttributeGroupDescriptionEdit();
087    }
088
089    @Override
090    public EntityAttributeGroupDescription getEntity(EditEntityAttributeGroupDescriptionResult result) {
091        EntityAttributeGroupDescription entityAttributeGroupDescription = null;
092        var entityAttributeGroupName = spec.getEntityAttributeGroupName();
093        var entityAttributeGroup = coreControl.getEntityAttributeGroupByName(entityAttributeGroupName);
094
095        if(entityAttributeGroup != null) {
096            var languageIsoName = spec.getLanguageIsoName();
097            var language = partyControl.getLanguageByIsoName(languageIsoName);
098
099            if(language != null) {
100                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
101                    entityAttributeGroupDescription = coreControl.getEntityAttributeGroupDescription(entityAttributeGroup, language);
102                } else { // EditMode.UPDATE
103                    entityAttributeGroupDescription = coreControl.getEntityAttributeGroupDescriptionForUpdate(entityAttributeGroup, language);
104                }
105
106                if(entityAttributeGroupDescription == null) {
107                    addExecutionError(ExecutionErrors.UnknownEntityAttributeGroupDescription.name(), entityAttributeGroupName, languageIsoName);
108                }
109            } else {
110                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
111            }
112        } else {
113            addExecutionError(ExecutionErrors.UnknownEntityAttributeGroupName.name(), entityAttributeGroupName);
114        }
115
116        return entityAttributeGroupDescription;
117    }
118
119    @Override
120    public EntityAttributeGroup getLockEntity(EntityAttributeGroupDescription entityAttributeGroupDescription) {
121        return entityAttributeGroupDescription.getEntityAttributeGroup();
122    }
123
124    @Override
125    public void fillInResult(EditEntityAttributeGroupDescriptionResult result, EntityAttributeGroupDescription entityAttributeGroupDescription) {
126
127        result.setEntityAttributeGroupDescription(coreControl.getEntityAttributeGroupDescriptionTransfer(getUserVisit(), entityAttributeGroupDescription, null));
128    }
129
130    @Override
131    public void doLock(EntityAttributeGroupDescriptionEdit edit, EntityAttributeGroupDescription entityAttributeGroupDescription) {
132        edit.setDescription(entityAttributeGroupDescription.getDescription());
133    }
134
135    @Override
136    public void doUpdate(EntityAttributeGroupDescription entityAttributeGroupDescription) {
137        var entityAttributeGroupDescriptionValue = coreControl.getEntityAttributeGroupDescriptionValue(entityAttributeGroupDescription);
138        
139        entityAttributeGroupDescriptionValue.setDescription(edit.getDescription());
140        
141        coreControl.updateEntityAttributeGroupDescriptionFromValue(entityAttributeGroupDescriptionValue, getPartyPK());
142    }
143
144    
145}