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.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.EntityAppearanceEdit; 021import com.echothree.control.user.core.common.form.EditEntityAppearanceForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditEntityAppearanceResult; 024import com.echothree.control.user.core.common.spec.EntityRefSpec; 025import com.echothree.model.control.core.server.logic.AppearanceLogic; 026import com.echothree.model.control.party.common.PartyTypes; 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.EntityAppearance; 031import com.echothree.model.data.core.server.entity.EntityInstance; 032import com.echothree.model.data.core.server.value.EntityAppearanceValue; 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.BaseResult; 038import com.echothree.util.common.command.EditMode; 039import com.echothree.util.common.persistence.BasePK; 040import com.echothree.util.server.control.BaseEditCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.PersistenceUtils; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class EditEntityAppearanceCommand 050 extends BaseEditCommand<EntityRefSpec, EntityAppearanceEdit> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 054 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 060 new SecurityRoleDefinition(SecurityRoleGroups.EntityAppearance.name(), SecurityRoles.Edit.name()) 061 ))) 062 ))); 063 064 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 065 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null) 066 )); 067 068 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 069 new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, true, null, null) 070 )); 071 } 072 073 /** Creates a new instance of EditEntityAppearanceCommand */ 074 public EditEntityAppearanceCommand(UserVisitPK userVisitPK, EditEntityAppearanceForm form) { 075 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 var coreControl = getCoreControl(); 081 EditEntityAppearanceResult result = CoreResultFactory.getEditEntityAppearanceResult(); 082 String entityRef = spec.getEntityRef(); 083 EntityInstance entityInstance = coreControl.getEntityInstanceByEntityRef(entityRef); 084 085 if(entityInstance != null) { 086 EntityAppearance entityAppearance = null; 087 BasePK basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 088 089 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 090 entityAppearance = coreControl.getEntityAppearance(entityInstance); 091 092 if(entityAppearance != null) { 093 if(editMode.equals(EditMode.LOCK)) { 094 result.setEntityAppearance(coreControl.getEntityAppearanceTransfer(getUserVisit(), entityAppearance)); 095 096 if(lockEntity(basePK)) { 097 EntityAppearanceEdit edit = CoreEditFactory.getEntityAppearanceEdit(); 098 099 result.setEdit(edit); 100 edit.setAppearanceName(entityAppearance.getAppearance().getLastDetail().getAppearanceName()); 101 } else { 102 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 103 } 104 } else { // EditMode.ABANDON 105 unlockEntity(basePK); 106 basePK = null; 107 } 108 } else { 109 addExecutionError(ExecutionErrors.UnknownEntityAppearance.name(), entityRef); 110 } 111 } else { 112 if(editMode.equals(EditMode.UPDATE)) { 113 String appearanceName = edit.getAppearanceName(); 114 Appearance appearance = AppearanceLogic.getInstance().getAppearanceByName(this, appearanceName); 115 116 if(!hasExecutionErrors()) { 117 entityAppearance = coreControl.getEntityAppearanceForUpdate(entityInstance); 118 119 if(entityAppearance != null) { 120 if(lockEntityForUpdate(basePK)) { 121 try { 122 EntityAppearanceValue entityAppearanceValue = coreControl.getEntityAppearanceValue(entityAppearance); 123 124 entityAppearanceValue.setAppearancePK(appearance.getPrimaryKey()); 125 126 coreControl.updateEntityAppearanceFromValue(entityAppearanceValue, getPartyPK()); 127 } finally { 128 unlockEntity(basePK); 129 basePK = null; 130 } 131 } else { 132 addExecutionError(ExecutionErrors.EntityLockStale.name()); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownEntityAppearance.name(), entityRef); 136 } 137 } 138 } 139 } 140 141 if(basePK != null) { 142 result.setEntityLock(getEntityLockTransfer(basePK)); 143 } 144 145 if(entityAppearance != null) { 146 result.setEntityAppearance(coreControl.getEntityAppearanceTransfer(getUserVisit(), entityAppearance)); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef); 150 } 151 152 return result; 153 } 154 155}