001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.CommandMessageDetail; 030import com.echothree.model.data.core.server.entity.CommandMessageTranslation; 031import com.echothree.model.data.core.server.entity.CommandMessageType; 032import com.echothree.model.data.core.server.value.CommandMessageDetailValue; 033import com.echothree.model.data.core.server.value.CommandMessageTranslationValue; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.message.ExecutionErrors; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.common.command.EditMode; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import java.util.Arrays; 044import java.util.Collections; 045import java.util.List; 046 047public class EditCommandMessageCommand 048 extends BaseAbstractEditCommand<CommandMessageSpec, CommandMessageEdit, EditCommandMessageResult, CommandMessage, CommandMessage> { 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.CommandMessage.name(), SecurityRoles.Edit.name()) 059 ))) 060 ))); 061 062 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("CommandMessageKey", FieldType.KEY, true, null, null) 064 )); 065 066 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("CommandMessageKey", FieldType.KEY, true, null, null), 068 new FieldDefinition("Translation", FieldType.STRING, false, 1L, 80L) 069 )); 070 } 071 072 /** Creates a new instance of EditCommandMessageCommand */ 073 public EditCommandMessageCommand(UserVisitPK userVisitPK, EditCommandMessageForm form) { 074 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 public EditCommandMessageResult getResult() { 079 return CoreResultFactory.getEditCommandMessageResult(); 080 } 081 082 @Override 083 public CommandMessageEdit getEdit() { 084 return CoreEditFactory.getCommandMessageEdit(); 085 } 086 087 CommandMessageType commandMessageType; 088 089 @Override 090 public CommandMessage getEntity(EditCommandMessageResult result) { 091 var coreControl = getCoreControl(); 092 CommandMessage commandMessage = null; 093 String commandMessageTypeName = spec.getCommandMessageTypeName(); 094 095 commandMessageType = coreControl.getCommandMessageTypeByName(commandMessageTypeName); 096 097 if(commandMessageType != null) { 098 String commandMessageKey = spec.getCommandMessageKey(); 099 100 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 101 commandMessage = coreControl.getCommandMessageByKey(commandMessageType, commandMessageKey); 102 } else { // EditMode.UPDATE 103 commandMessage = coreControl.getCommandMessageByKeyForUpdate(commandMessageType, commandMessageKey); 104 } 105 106 if(commandMessage != null) { 107 result.setCommandMessage(coreControl.getCommandMessageTransfer(getUserVisit(), commandMessage)); 108 } else { 109 addExecutionError(ExecutionErrors.UnknownCommandMessageKey.name(), commandMessageTypeName, commandMessageKey); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.UnknownCommandMessageTypeName.name(), commandMessageTypeName); 113 } 114 115 return commandMessage; 116 } 117 118 @Override 119 public CommandMessage getLockEntity(CommandMessage commandMessage) { 120 return commandMessage; 121 } 122 123 @Override 124 public void fillInResult(EditCommandMessageResult result, CommandMessage commandMessage) { 125 var coreControl = getCoreControl(); 126 127 result.setCommandMessage(coreControl.getCommandMessageTransfer(getUserVisit(), commandMessage)); 128 } 129 130 @Override 131 public void doLock(CommandMessageEdit edit, CommandMessage commandMessage) { 132 var coreControl = getCoreControl(); 133 CommandMessageTranslation commandMessageTranslation = coreControl.getCommandMessageTranslation(commandMessage, getPreferredLanguage()); 134 CommandMessageDetail commandMessageDetail = commandMessage.getLastDetail(); 135 136 edit.setCommandMessageKey(commandMessageDetail.getCommandMessageKey()); 137 138 if(commandMessageTranslation != null) { 139 edit.setTranslation(commandMessageTranslation.getTranslation()); 140 } 141 } 142 143 @Override 144 public void canUpdate(CommandMessage commandMessage) { 145 var coreControl = getCoreControl(); 146 String commandMessageKey = edit.getCommandMessageKey(); 147 CommandMessage duplicateCommandMessage = coreControl.getCommandMessageByKey(commandMessageType, commandMessageKey); 148 149 if(duplicateCommandMessage != null && !commandMessage.equals(duplicateCommandMessage)) { 150 addExecutionError(ExecutionErrors.DuplicateCommandMessageKey.name(), commandMessageType.getLastDetail().getCommandMessageTypeName(), commandMessageKey); 151 } 152 } 153 154 @Override 155 public void doUpdate(CommandMessage commandMessage) { 156 var coreControl = getCoreControl(); 157 var partyPK = getPartyPK(); 158 CommandMessageDetailValue commandMessageDetailValue = coreControl.getCommandMessageDetailValueForUpdate(commandMessage); 159 CommandMessageTranslation commandMessageTranslation = coreControl.getCommandMessageTranslationForUpdate(commandMessage, getPreferredLanguage()); 160 String translation = edit.getTranslation(); 161 162 commandMessageDetailValue.setCommandMessageKey(edit.getCommandMessageKey()); 163 164 coreControl.updateCommandMessageFromValue(commandMessageDetailValue, partyPK); 165 166 if(commandMessageTranslation == null && translation != null) { 167 coreControl.createCommandMessageTranslation(commandMessage, getPreferredLanguage(), translation, partyPK); 168 } else { 169 if(commandMessageTranslation != null && translation == null) { 170 coreControl.deleteCommandMessageTranslation(commandMessageTranslation, partyPK); 171 } else { 172 if(commandMessageTranslation != null && translation != null) { 173 CommandMessageTranslationValue commandMessageTranslationValue = coreControl.getCommandMessageTranslationValue(commandMessageTranslation); 174 175 commandMessageTranslationValue.setTranslation(translation); 176 coreControl.updateCommandMessageTranslationFromValue(commandMessageTranslationValue, partyPK); 177 } 178 } 179 } 180 } 181 182}