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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.AppearanceDescriptionEdit; 020import com.echothree.control.user.core.common.edit.CoreEditFactory; 021import com.echothree.control.user.core.common.form.EditAppearanceDescriptionForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditAppearanceDescriptionResult; 024import com.echothree.control.user.core.common.spec.AppearanceDescriptionSpec; 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.data.core.server.entity.Appearance; 030import com.echothree.model.data.core.server.entity.AppearanceDescription; 031import com.echothree.model.data.core.server.value.AppearanceDescriptionValue; 032import com.echothree.model.data.party.server.entity.Language; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.common.command.EditMode; 038import com.echothree.util.server.control.BaseAbstractEditCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import com.echothree.util.server.control.SecurityRoleDefinition; 042import com.echothree.util.server.persistence.Session; 043import java.util.Arrays; 044import java.util.Collections; 045import java.util.List; 046 047public class EditAppearanceDescriptionCommand 048 extends BaseAbstractEditCommand<AppearanceDescriptionSpec, AppearanceDescriptionEdit, EditAppearanceDescriptionResult, AppearanceDescription, Appearance> { 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.Appearance.name(), SecurityRoles.Description.name()) 059 ))) 060 ))); 061 062 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 065 )); 066 067 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 068 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 069 )); 070 } 071 072 /** Creates a new instance of EditAppearanceDescriptionCommand */ 073 public EditAppearanceDescriptionCommand(UserVisitPK userVisitPK, EditAppearanceDescriptionForm form) { 074 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 public EditAppearanceDescriptionResult getResult() { 079 return CoreResultFactory.getEditAppearanceDescriptionResult(); 080 } 081 082 @Override 083 public AppearanceDescriptionEdit getEdit() { 084 return CoreEditFactory.getAppearanceDescriptionEdit(); 085 } 086 087 @Override 088 public AppearanceDescription getEntity(EditAppearanceDescriptionResult result) { 089 var coreControl = getCoreControl(); 090 AppearanceDescription appearanceDescription = null; 091 String appearanceName = spec.getAppearanceName(); 092 Appearance appearance = coreControl.getAppearanceByName(appearanceName); 093 094 if(appearance != null) { 095 var partyControl = Session.getModelController(PartyControl.class); 096 String languageIsoName = spec.getLanguageIsoName(); 097 Language language = partyControl.getLanguageByIsoName(languageIsoName); 098 099 if(language != null) { 100 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 101 appearanceDescription = coreControl.getAppearanceDescription(appearance, language); 102 } else { // EditMode.UPDATE 103 appearanceDescription = coreControl.getAppearanceDescriptionForUpdate(appearance, language); 104 } 105 106 if(appearanceDescription == null) { 107 addExecutionError(ExecutionErrors.UnknownAppearanceDescription.name(), appearanceName, languageIsoName); 108 } 109 } else { 110 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.UnknownAppearanceName.name(), appearanceName); 114 } 115 116 return appearanceDescription; 117 } 118 119 @Override 120 public Appearance getLockEntity(AppearanceDescription appearanceDescription) { 121 return appearanceDescription.getAppearance(); 122 } 123 124 @Override 125 public void fillInResult(EditAppearanceDescriptionResult result, AppearanceDescription appearanceDescription) { 126 var coreControl = getCoreControl(); 127 128 result.setAppearanceDescription(coreControl.getAppearanceDescriptionTransfer(getUserVisit(), appearanceDescription)); 129 } 130 131 @Override 132 public void doLock(AppearanceDescriptionEdit edit, AppearanceDescription appearanceDescription) { 133 edit.setDescription(appearanceDescription.getDescription()); 134 } 135 136 @Override 137 public void doUpdate(AppearanceDescription appearanceDescription) { 138 var coreControl = getCoreControl(); 139 AppearanceDescriptionValue appearanceDescriptionValue = coreControl.getAppearanceDescriptionValue(appearanceDescription); 140 appearanceDescriptionValue.setDescription(edit.getDescription()); 141 142 coreControl.updateAppearanceDescriptionFromValue(appearanceDescriptionValue, getPartyPK()); 143 } 144 145}