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