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