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.core.server.control.CommandControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.core.server.entity.CommandMessage; 030import com.echothree.model.data.core.server.entity.CommandMessageType; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 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 com.echothree.util.server.persistence.Session; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043 044@Dependent 045public class EditCommandMessageCommand 046 extends BaseAbstractEditCommand<CommandMessageSpec, CommandMessageEdit, EditCommandMessageResult, CommandMessage, CommandMessage> { 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.CommandMessage.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("CommandMessageKey", FieldType.KEY, true, null, null) 062 ); 063 064 EDIT_FIELD_DEFINITIONS = List.of( 065 new FieldDefinition("CommandMessageKey", FieldType.KEY, true, null, null), 066 new FieldDefinition("Translation", FieldType.STRING, false, 1L, 80L) 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 var commandControl = Session.getModelController(CommandControl.class); 090 CommandMessage commandMessage = null; 091 var commandMessageTypeName = spec.getCommandMessageTypeName(); 092 093 commandMessageType = commandControl.getCommandMessageTypeByName(commandMessageTypeName); 094 095 if(commandMessageType != null) { 096 var commandMessageKey = spec.getCommandMessageKey(); 097 098 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 099 commandMessage = commandControl.getCommandMessageByKey(commandMessageType, commandMessageKey); 100 } else { // EditMode.UPDATE 101 commandMessage = commandControl.getCommandMessageByKeyForUpdate(commandMessageType, commandMessageKey); 102 } 103 104 if(commandMessage != null) { 105 result.setCommandMessage(commandControl.getCommandMessageTransfer(getUserVisit(), commandMessage)); 106 } else { 107 addExecutionError(ExecutionErrors.UnknownCommandMessageKey.name(), commandMessageTypeName, commandMessageKey); 108 } 109 } else { 110 addExecutionError(ExecutionErrors.UnknownCommandMessageTypeName.name(), commandMessageTypeName); 111 } 112 113 return commandMessage; 114 } 115 116 @Override 117 public CommandMessage getLockEntity(CommandMessage commandMessage) { 118 return commandMessage; 119 } 120 121 @Override 122 public void fillInResult(EditCommandMessageResult result, CommandMessage commandMessage) { 123 var commandControl = Session.getModelController(CommandControl.class); 124 125 result.setCommandMessage(commandControl.getCommandMessageTransfer(getUserVisit(), commandMessage)); 126 } 127 128 @Override 129 public void doLock(CommandMessageEdit edit, CommandMessage commandMessage) { 130 var commandControl = Session.getModelController(CommandControl.class); 131 var commandMessageTranslation = commandControl.getCommandMessageTranslation(commandMessage, getPreferredLanguage()); 132 var commandMessageDetail = commandMessage.getLastDetail(); 133 134 edit.setCommandMessageKey(commandMessageDetail.getCommandMessageKey()); 135 136 if(commandMessageTranslation != null) { 137 edit.setTranslation(commandMessageTranslation.getTranslation()); 138 } 139 } 140 141 @Override 142 public void canUpdate(CommandMessage commandMessage) { 143 var commandControl = Session.getModelController(CommandControl.class); 144 var commandMessageKey = edit.getCommandMessageKey(); 145 var duplicateCommandMessage = commandControl.getCommandMessageByKey(commandMessageType, commandMessageKey); 146 147 if(duplicateCommandMessage != null && !commandMessage.equals(duplicateCommandMessage)) { 148 addExecutionError(ExecutionErrors.DuplicateCommandMessageKey.name(), commandMessageType.getLastDetail().getCommandMessageTypeName(), commandMessageKey); 149 } 150 } 151 152 @Override 153 public void doUpdate(CommandMessage commandMessage) { 154 var commandControl = Session.getModelController(CommandControl.class); 155 var partyPK = getPartyPK(); 156 var commandMessageDetailValue = commandControl.getCommandMessageDetailValueForUpdate(commandMessage); 157 var commandMessageTranslation = commandControl.getCommandMessageTranslationForUpdate(commandMessage, getPreferredLanguage()); 158 var translation = edit.getTranslation(); 159 160 commandMessageDetailValue.setCommandMessageKey(edit.getCommandMessageKey()); 161 162 commandControl.updateCommandMessageFromValue(commandMessageDetailValue, partyPK); 163 164 if(commandMessageTranslation == null && translation != null) { 165 commandControl.createCommandMessageTranslation(commandMessage, getPreferredLanguage(), translation, partyPK); 166 } else { 167 if(commandMessageTranslation != null && translation == null) { 168 commandControl.deleteCommandMessageTranslation(commandMessageTranslation, partyPK); 169 } else { 170 if(commandMessageTranslation != null && translation != null) { 171 var commandMessageTranslationValue = commandControl.getCommandMessageTranslationValue(commandMessageTranslation); 172 173 commandMessageTranslationValue.setTranslation(translation); 174 commandControl.updateCommandMessageTranslationFromValue(commandMessageTranslationValue, partyPK); 175 } 176 } 177 } 178 } 179 180}