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.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 com.echothree.util.server.persistence.Session;
041import java.util.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class EditEntityAttributeGroupDescriptionCommand
048        extends BaseAbstractEditCommand<EntityAttributeGroupDescriptionSpec, EntityAttributeGroupDescriptionEdit, EditEntityAttributeGroupDescriptionResult, EntityAttributeGroupDescription, EntityAttributeGroup> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.EntityAttributeGroup.name(), SecurityRoles.Description.name())
059                        )))
060                )));
061        
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
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 = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
069                ));
070    }
071    
072    /** Creates a new instance of EditEntityAttributeGroupDescriptionCommand */
073    public EditEntityAttributeGroupDescriptionCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076
077    @Override
078    public EditEntityAttributeGroupDescriptionResult getResult() {
079        return CoreResultFactory.getEditEntityAttributeGroupDescriptionResult();
080    }
081
082    @Override
083    public EntityAttributeGroupDescriptionEdit getEdit() {
084        return CoreEditFactory.getEntityAttributeGroupDescriptionEdit();
085    }
086
087    @Override
088    public EntityAttributeGroupDescription getEntity(EditEntityAttributeGroupDescriptionResult result) {
089        EntityAttributeGroupDescription entityAttributeGroupDescription = null;
090        var entityAttributeGroupName = spec.getEntityAttributeGroupName();
091        var entityAttributeGroup = coreControl.getEntityAttributeGroupByName(entityAttributeGroupName);
092
093        if(entityAttributeGroup != null) {
094            var partyControl = Session.getModelController(PartyControl.class);
095            var languageIsoName = spec.getLanguageIsoName();
096            var language = partyControl.getLanguageByIsoName(languageIsoName);
097
098            if(language != null) {
099                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
100                    entityAttributeGroupDescription = coreControl.getEntityAttributeGroupDescription(entityAttributeGroup, language);
101                } else { // EditMode.UPDATE
102                    entityAttributeGroupDescription = coreControl.getEntityAttributeGroupDescriptionForUpdate(entityAttributeGroup, language);
103                }
104
105                if(entityAttributeGroupDescription == null) {
106                    addExecutionError(ExecutionErrors.UnknownEntityAttributeGroupDescription.name(), entityAttributeGroupName, languageIsoName);
107                }
108            } else {
109                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
110            }
111        } else {
112            addExecutionError(ExecutionErrors.UnknownEntityAttributeGroupName.name(), entityAttributeGroupName);
113        }
114
115        return entityAttributeGroupDescription;
116    }
117
118    @Override
119    public EntityAttributeGroup getLockEntity(EntityAttributeGroupDescription entityAttributeGroupDescription) {
120        return entityAttributeGroupDescription.getEntityAttributeGroup();
121    }
122
123    @Override
124    public void fillInResult(EditEntityAttributeGroupDescriptionResult result, EntityAttributeGroupDescription entityAttributeGroupDescription) {
125
126        result.setEntityAttributeGroupDescription(coreControl.getEntityAttributeGroupDescriptionTransfer(getUserVisit(), entityAttributeGroupDescription, null));
127    }
128
129    @Override
130    public void doLock(EntityAttributeGroupDescriptionEdit edit, EntityAttributeGroupDescription entityAttributeGroupDescription) {
131        edit.setDescription(entityAttributeGroupDescription.getDescription());
132    }
133
134    @Override
135    public void doUpdate(EntityAttributeGroupDescription entityAttributeGroupDescription) {
136        var entityAttributeGroupDescriptionValue = coreControl.getEntityAttributeGroupDescriptionValue(entityAttributeGroupDescription);
137        
138        entityAttributeGroupDescriptionValue.setDescription(edit.getDescription());
139        
140        coreControl.updateEntityAttributeGroupDescriptionFromValue(entityAttributeGroupDescriptionValue, getPartyPK());
141    }
142
143    
144}