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.CommandMessageEdit;
020import com.echothree.control.user.core.common.edit.CoreEditFactory;
021import com.echothree.control.user.core.common.form.EditCommandMessageForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditCommandMessageResult;
024import com.echothree.control.user.core.common.spec.CommandMessageSpec;
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.CommandMessage;
029import com.echothree.model.data.core.server.entity.CommandMessageType;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.EditMode;
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 EditCommandMessageCommand
045        extends BaseAbstractEditCommand<CommandMessageSpec, CommandMessageEdit, EditCommandMessageResult, CommandMessage, CommandMessage> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.CommandMessage.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058        
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("CommandMessageKey", FieldType.KEY, true, null, null)
061        );
062        
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("CommandMessageKey", FieldType.KEY, true, null, null),
065                new FieldDefinition("Translation", FieldType.STRING, false, 1L, 80L)
066        );
067    }
068
069    
070    /** Creates a new instance of EditCommandMessageCommand */
071    public EditCommandMessageCommand() {
072        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
073    }
074    
075    @Override
076    public EditCommandMessageResult getResult() {
077        return CoreResultFactory.getEditCommandMessageResult();
078    }
079
080    @Override
081    public CommandMessageEdit getEdit() {
082        return CoreEditFactory.getCommandMessageEdit();
083    }
084
085    CommandMessageType commandMessageType;
086
087    @Override
088    public CommandMessage getEntity(EditCommandMessageResult result) {
089        CommandMessage commandMessage = null;
090        var commandMessageTypeName = spec.getCommandMessageTypeName();
091
092        commandMessageType = commandControl.getCommandMessageTypeByName(commandMessageTypeName);
093
094        if(commandMessageType != null) {
095            var commandMessageKey = spec.getCommandMessageKey();
096
097            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
098                commandMessage = commandControl.getCommandMessageByKey(commandMessageType, commandMessageKey);
099            } else { // EditMode.UPDATE
100                commandMessage = commandControl.getCommandMessageByKeyForUpdate(commandMessageType, commandMessageKey);
101            }
102
103            if(commandMessage != null) {
104                result.setCommandMessage(commandControl.getCommandMessageTransfer(getUserVisit(), commandMessage));
105            } else {
106                addExecutionError(ExecutionErrors.UnknownCommandMessageKey.name(), commandMessageTypeName, commandMessageKey);
107            }
108        } else {
109            addExecutionError(ExecutionErrors.UnknownCommandMessageTypeName.name(), commandMessageTypeName);
110        }
111
112        return commandMessage;
113    }
114
115    @Override
116    public CommandMessage getLockEntity(CommandMessage commandMessage) {
117        return commandMessage;
118    }
119
120    @Override
121    public void fillInResult(EditCommandMessageResult result, CommandMessage commandMessage) {
122        result.setCommandMessage(commandControl.getCommandMessageTransfer(getUserVisit(), commandMessage));
123    }
124
125    @Override
126    public void doLock(CommandMessageEdit edit, CommandMessage commandMessage) {
127        var commandMessageTranslation = commandControl.getCommandMessageTranslation(commandMessage, getPreferredLanguage());
128        var commandMessageDetail = commandMessage.getLastDetail();
129
130        edit.setCommandMessageKey(commandMessageDetail.getCommandMessageKey());
131
132        if(commandMessageTranslation != null) {
133            edit.setTranslation(commandMessageTranslation.getTranslation());
134        }
135    }
136
137    @Override
138    public void canUpdate(CommandMessage commandMessage) {
139        var commandMessageKey = edit.getCommandMessageKey();
140        var duplicateCommandMessage = commandControl.getCommandMessageByKey(commandMessageType, commandMessageKey);
141
142        if(duplicateCommandMessage != null && !commandMessage.equals(duplicateCommandMessage)) {
143            addExecutionError(ExecutionErrors.DuplicateCommandMessageKey.name(), commandMessageType.getLastDetail().getCommandMessageTypeName(), commandMessageKey);
144        }
145    }
146
147    @Override
148    public void doUpdate(CommandMessage commandMessage) {
149        var partyPK = getPartyPK();
150        var commandMessageDetailValue = commandControl.getCommandMessageDetailValueForUpdate(commandMessage);
151        var commandMessageTranslation = commandControl.getCommandMessageTranslationForUpdate(commandMessage, getPreferredLanguage());
152        var translation = edit.getTranslation();
153
154        commandMessageDetailValue.setCommandMessageKey(edit.getCommandMessageKey());
155
156        commandControl.updateCommandMessageFromValue(commandMessageDetailValue, partyPK);
157
158        if(commandMessageTranslation == null && translation != null) {
159            commandControl.createCommandMessageTranslation(commandMessage, getPreferredLanguage(), translation, partyPK);
160        } else {
161            if(commandMessageTranslation != null && translation == null) {
162                commandControl.deleteCommandMessageTranslation(commandMessageTranslation, partyPK);
163            } else {
164                if(commandMessageTranslation != null && translation != null) {
165                    var commandMessageTranslationValue = commandControl.getCommandMessageTranslationValue(commandMessageTranslation);
166
167                    commandMessageTranslationValue.setTranslation(translation);
168                    commandControl.updateCommandMessageTranslationFromValue(commandMessageTranslationValue, partyPK);
169                }
170            }
171        }
172    }
173    
174}