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.selector.server.command; 018 019import com.echothree.control.user.selector.common.edit.SelectorEditFactory; 020import com.echothree.control.user.selector.common.edit.SelectorNodeDescriptionEdit; 021import com.echothree.control.user.selector.common.form.EditSelectorNodeDescriptionForm; 022import com.echothree.control.user.selector.common.result.SelectorResultFactory; 023import com.echothree.control.user.selector.common.spec.SelectorNodeDescriptionSpec; 024import com.echothree.model.control.party.server.control.PartyControl; 025import com.echothree.model.control.selector.server.control.SelectorControl; 026import com.echothree.model.data.user.common.pk.UserVisitPK; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.command.EditMode; 032import com.echothree.util.server.control.BaseEditCommand; 033import com.echothree.util.server.persistence.Session; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036 037@Dependent 038public class EditSelectorNodeDescriptionCommand 039 extends BaseEditCommand<SelectorNodeDescriptionSpec, SelectorNodeDescriptionEdit> { 040 041 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 042 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 043 044 static { 045 SPEC_FIELD_DEFINITIONS = List.of( 046 new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("SelectorNodeName", FieldType.ENTITY_NAME, true, null, null), 050 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 051 ); 052 053 EDIT_FIELD_DEFINITIONS = List.of( 054 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 055 ); 056 } 057 058 /** Creates a new instance of EditSelectorNodeDescriptionCommand */ 059 public EditSelectorNodeDescriptionCommand() { 060 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 061 } 062 063 @Override 064 protected BaseResult execute() { 065 var selectorControl = Session.getModelController(SelectorControl.class); 066 var result = SelectorResultFactory.getEditSelectorNodeDescriptionResult(); 067 var selectorKindName = spec.getSelectorKindName(); 068 var selectorKind = selectorControl.getSelectorKindByName(selectorKindName); 069 070 if(selectorKind != null) { 071 var selectorTypeName = spec.getSelectorTypeName(); 072 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName); 073 074 if(selectorType != null) { 075 var selectorName = spec.getSelectorName(); 076 var selector = selectorControl.getSelectorByName(selectorType, selectorName); 077 078 if(selector != null) { 079 var selectorNodeName = spec.getSelectorNodeName(); 080 var selectorNode = selectorControl.getSelectorNodeByName(selector, selectorNodeName); 081 082 if(selectorNode != null) { 083 var partyControl = Session.getModelController(PartyControl.class); 084 var languageIsoName = spec.getLanguageIsoName(); 085 var language = partyControl.getLanguageByIsoName(languageIsoName); 086 087 if(language != null) { 088 if(editMode.equals(EditMode.LOCK)) { 089 var selectorNodeDescription = selectorControl.getSelectorNodeDescription(selectorNode, language); 090 091 if(selectorNodeDescription != null) { 092 result.setSelectorNodeDescription(selectorControl.getSelectorNodeDescriptionTransfer(getUserVisit(), selectorNodeDescription)); 093 094 if(lockEntity(selectorNode)) { 095 var edit = SelectorEditFactory.getSelectorNodeDescriptionEdit(); 096 097 result.setEdit(edit); 098 edit.setDescription(selectorNodeDescription.getDescription()); 099 } else { 100 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 101 } 102 103 result.setEntityLock(getEntityLockTransfer(selectorNode)); 104 } else { 105 addExecutionError(ExecutionErrors.UnknownSelectorNodeDescription.name()); 106 } 107 } else if(editMode.equals(EditMode.UPDATE)) { 108 var selectorNodeDescriptionValue = selectorControl.getSelectorNodeDescriptionValueForUpdate(selectorNode, language); 109 110 if(selectorNodeDescriptionValue != null) { 111 if(lockEntityForUpdate(selectorNode)) { 112 try { 113 var description = edit.getDescription(); 114 115 selectorNodeDescriptionValue.setDescription(description); 116 117 selectorControl.updateSelectorNodeDescriptionFromValue(selectorNodeDescriptionValue, getPartyPK()); 118 } finally { 119 unlockEntity(selectorNode); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.EntityLockStale.name()); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownSelectorNodeDescription.name()); 126 } 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownSelectorNodeName.name(), selectorNodeName); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName); 142 } 143 144 return result; 145 } 146 147}