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.cancellationpolicy.server.command;
018
019import com.echothree.control.user.cancellationpolicy.common.edit.CancellationKindDescriptionEdit;
020import com.echothree.control.user.cancellationpolicy.common.edit.CancellationPolicyEditFactory;
021import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory;
022import com.echothree.control.user.cancellationpolicy.common.result.EditCancellationKindDescriptionResult;
023import com.echothree.control.user.cancellationpolicy.common.spec.CancellationKindDescriptionSpec;
024import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
025import com.echothree.model.control.cancellationpolicy.server.logic.CancellationKindLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.logic.LanguageLogic;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKind;
031import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKindDescription;
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.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditCancellationKindDescriptionCommand
045        extends BaseAbstractEditCommand<CancellationKindDescriptionSpec, CancellationKindDescriptionEdit, EditCancellationKindDescriptionResult,
046                CancellationKindDescription, CancellationKind> {
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.CancellationKind.name(), SecurityRoles.Description.name())
057                ))
058        ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("CancellationKindName", 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    CancellationPolicyControl cancellationPolicyControl;
072
073    @Inject
074    CancellationKindLogic cancellationKindLogic;
075
076    @Inject
077    LanguageLogic languageLogic;
078    
079    /** Creates a new instance of EditCancellationKindDescriptionCommand */
080    public EditCancellationKindDescriptionCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083    
084    @Override
085    public EditCancellationKindDescriptionResult getResult() {
086        return CancellationPolicyResultFactory.getEditCancellationKindDescriptionResult();
087    }
088
089    @Override
090    public CancellationKindDescriptionEdit getEdit() {
091        return CancellationPolicyEditFactory.getCancellationKindDescriptionEdit();
092    }
093
094    @Override
095    public CancellationKindDescription getEntity(EditCancellationKindDescriptionResult result) {
096        CancellationKindDescription cancellationKindDescription = null;
097        var cancellationKindName = spec.getCancellationKindName();
098        var cancellationKind = cancellationKindLogic.getCancellationKindByName(this, cancellationKindName);
099        
100        if(!hasExecutionErrors()) {
101            var languageIsoName = spec.getLanguageIsoName();
102            var language = languageLogic.getLanguageByName(this, languageIsoName);
103            
104            if(!hasExecutionErrors()) {
105                cancellationKindDescription = cancellationPolicyControl.getCancellationKindDescription(cancellationKind, language,
106                        editModeToEntityPermission(editMode));
107
108                if(cancellationKindDescription == null) {
109                    addExecutionError(ExecutionErrors.UnknownCancellationKindDescription.name(), cancellationKindName, languageIsoName);
110                }
111            }
112        }
113
114        return cancellationKindDescription;
115    }
116
117    @Override
118    public CancellationKind getLockEntity(CancellationKindDescription cancellationKindDescription) {
119        return cancellationKindDescription.getCancellationKind();
120    }
121
122    @Override
123    public void fillInResult(EditCancellationKindDescriptionResult result, CancellationKindDescription cancellationKindDescription) {
124        result.setCancellationKindDescription(cancellationPolicyControl.getCancellationKindDescriptionTransfer(getUserVisit(), cancellationKindDescription));
125    }
126
127    @Override
128    public void doLock(CancellationKindDescriptionEdit edit, CancellationKindDescription cancellationKindDescription) {
129        edit.setDescription(cancellationKindDescription.getDescription());
130    }
131
132    @Override
133    public void doUpdate(CancellationKindDescription cancellationKindDescription) {
134        var cancellationKindDescriptionValue = cancellationPolicyControl.getCancellationKindDescriptionValue(cancellationKindDescription);
135
136        cancellationKindDescriptionValue.setDescription(edit.getDescription());
137
138        cancellationPolicyControl.updateCancellationKindDescriptionFromValue(cancellationKindDescriptionValue, getPartyPK());
139    }
140    
141}