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.message.server.command; 018 019import com.echothree.control.user.message.common.edit.MessageDescriptionEdit; 020import com.echothree.control.user.message.common.edit.MessageEditFactory; 021import com.echothree.control.user.message.common.form.EditMessageDescriptionForm; 022import com.echothree.control.user.message.common.result.MessageResultFactory; 023import com.echothree.control.user.message.common.spec.MessageDescriptionSpec; 024import com.echothree.model.control.message.server.control.MessageControl; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.model.data.user.common.pk.UserVisitPK; 027import com.echothree.util.common.command.BaseResult; 028import com.echothree.util.common.command.EditMode; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BaseEditCommand; 033import com.echothree.util.server.persistence.Session; 034import java.util.ArrayList; 035import java.util.Collections; 036import java.util.List; 037import javax.enterprise.context.RequestScoped; 038 039@RequestScoped 040public class EditMessageDescriptionCommand 041 extends BaseEditCommand<MessageDescriptionSpec, MessageDescriptionEdit> { 042 043 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 044 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 045 046 static { 047 List<FieldDefinition> temp = new ArrayList<>(4); 048 temp.add(new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null)); 049 temp.add(new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null)); 050 temp.add(new FieldDefinition("MessageName", FieldType.ENTITY_NAME, true, null, null)); 051 temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)); 052 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 053 054 temp = new ArrayList<>(1); 055 temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)); 056 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 057 } 058 059 /** Creates a new instance of EditMessageDescriptionCommand */ 060 public EditMessageDescriptionCommand() { 061 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 062 } 063 064 @Override 065 protected BaseResult execute() { 066 var result = MessageResultFactory.getEditMessageDescriptionResult(); 067 var componentVendorName = spec.getComponentVendorName(); 068 var componentVendor = componentControl.getComponentVendorByName(componentVendorName); 069 070 if(componentVendor != null) { 071 var entityTypeName = spec.getEntityTypeName(); 072 var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName); 073 074 if(entityType != null) { 075 var messageControl = Session.getModelController(MessageControl.class); 076 var messageTypeName = spec.getMessageTypeName(); 077 var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName); 078 079 if(messageType != null) { 080 var messageName = spec.getMessageName(); 081 var message = messageControl.getMessageByName(messageType, messageName); 082 083 if(message != null) { 084 var partyControl = Session.getModelController(PartyControl.class); 085 var languageIsoName = spec.getLanguageIsoName(); 086 var language = partyControl.getLanguageByIsoName(languageIsoName); 087 088 if(language != null) { 089 if(editMode.equals(EditMode.LOCK)) { 090 var messageDescription = messageControl.getMessageDescription(message, language); 091 092 if(messageDescription != null) { 093 result.setMessageDescription(messageControl.getMessageDescriptionTransfer(getUserVisit(), messageDescription)); 094 095 if(lockEntity(message)) { 096 var edit = MessageEditFactory.getMessageDescriptionEdit(); 097 098 result.setEdit(edit); 099 edit.setDescription(messageDescription.getDescription()); 100 } else { 101 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 102 } 103 104 result.setEntityLock(getEntityLockTransfer(message)); 105 } else { 106 addExecutionError(ExecutionErrors.UnknownMessageDescription.name()); 107 } 108 } else if(editMode.equals(EditMode.UPDATE)) { 109 var messageDescriptionValue = messageControl.getMessageDescriptionValueForUpdate(message, language); 110 111 if(messageDescriptionValue != null) { 112 if(lockEntityForUpdate(message)) { 113 try { 114 var description = edit.getDescription(); 115 116 messageDescriptionValue.setDescription(description); 117 118 messageControl.updateMessageDescriptionFromValue(messageDescriptionValue, getPartyPK()); 119 } finally { 120 unlockEntity(message); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.EntityLockStale.name()); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownMessageDescription.name()); 127 } 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName); 134 } 135 } else { 136 addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName); 137 } 138 } else { 139 addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName); 143 } 144 145 return result; 146 } 147 148}