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.letter.server.command; 018 019import com.echothree.control.user.letter.common.edit.LetterDescriptionEdit; 020import com.echothree.control.user.letter.common.edit.LetterEditFactory; 021import com.echothree.control.user.letter.common.form.EditLetterDescriptionForm; 022import com.echothree.control.user.letter.common.result.LetterResultFactory; 023import com.echothree.control.user.letter.common.spec.LetterDescriptionSpec; 024import com.echothree.model.control.chain.server.control.ChainControl; 025import com.echothree.model.control.letter.server.control.LetterControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.server.control.BaseEditCommand; 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.Arrays; 042import java.util.Collections; 043import java.util.List; 044import javax.enterprise.context.RequestScoped; 045 046@RequestScoped 047public class EditLetterDescriptionCommand 048 extends BaseEditCommand<LetterDescriptionSpec, LetterDescriptionEdit> { 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.Letter.name(), SecurityRoles.Description.name()) 059 ))) 060 ))); 061 062 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("LetterName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 067 )); 068 069 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 070 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 071 )); 072 } 073 074 /** Creates a new instance of EditLetterDescriptionCommand */ 075 public EditLetterDescriptionCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 protected BaseResult execute() { 081 var chainControl = Session.getModelController(ChainControl.class); 082 var result = LetterResultFactory.getEditLetterDescriptionResult(); 083 var chainKindName = spec.getChainKindName(); 084 var chainKind = chainControl.getChainKindByName(chainKindName); 085 086 if(chainKind != null) { 087 var chainTypeName = spec.getChainTypeName(); 088 var chainType = chainControl.getChainTypeByName(chainKind, chainTypeName); 089 090 if(chainType != null) { 091 var letterControl = Session.getModelController(LetterControl.class); 092 var letterName = spec.getLetterName(); 093 var letter = letterControl.getLetterByName(chainType, letterName); 094 095 if(letter != null) { 096 var partyControl = Session.getModelController(PartyControl.class); 097 var languageIsoName = spec.getLanguageIsoName(); 098 var language = partyControl.getLanguageByIsoName(languageIsoName); 099 100 if(language != null) { 101 if(editMode.equals(EditMode.LOCK)) { 102 var letterDescription = letterControl.getLetterDescription(letter, language); 103 104 if(letterDescription != null) { 105 result.setLetterDescription(letterControl.getLetterDescriptionTransfer(getUserVisit(), letterDescription)); 106 107 if(lockEntity(letter)) { 108 var edit = LetterEditFactory.getLetterDescriptionEdit(); 109 110 result.setEdit(edit); 111 edit.setDescription(letterDescription.getDescription()); 112 } else { 113 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 114 } 115 116 result.setEntityLock(getEntityLockTransfer(letter)); 117 } else { 118 addExecutionError(ExecutionErrors.UnknownLetterDescription.name()); 119 } 120 } else if(editMode.equals(EditMode.UPDATE)) { 121 var letterDescriptionValue = letterControl.getLetterDescriptionValueForUpdate(letter, language); 122 123 if(letterDescriptionValue != null) { 124 if(lockEntityForUpdate(letter)) { 125 try { 126 var description = edit.getDescription(); 127 128 letterDescriptionValue.setDescription(description); 129 130 letterControl.updateLetterDescriptionFromValue(letterDescriptionValue, getPartyPK()); 131 } finally { 132 unlockEntity(letter); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.EntityLockStale.name()); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownLetterDescription.name()); 139 } 140 } 141 } else { 142 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 143 } 144 } else { 145 addExecutionError(ExecutionErrors.UnknownLetterName.name(), letterName); 146 } 147 } else { 148 addExecutionError(ExecutionErrors.UnknownChainTypeName.name(), chainTypeName); 149 } 150 } else { 151 addExecutionError(ExecutionErrors.UnknownChainKindName.name(), chainKindName); 152 } 153 154 return result; 155 } 156 157}