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.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.spec.EntityRefSpec; 024import com.echothree.model.control.core.server.control.AppearanceControl; 025import com.echothree.model.control.core.server.control.EntityInstanceControl; 026import com.echothree.model.control.core.server.logic.AppearanceLogic; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.core.server.entity.EntityAppearance; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.command.EditMode; 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.server.control.BaseEditCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.PersistenceUtils; 042import com.echothree.util.server.persistence.Session; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045 046@Dependent 047public class EditEntityAppearanceCommand 048 extends BaseEditCommand<EntityRefSpec, EntityAppearanceEdit> { 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(List.of( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 058 new SecurityRoleDefinition(SecurityRoleGroups.EntityAppearance.name(), SecurityRoles.Edit.name()) 059 )) 060 )); 061 062 SPEC_FIELD_DEFINITIONS = List.of( 063 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, true, null, null) 064 ); 065 066 EDIT_FIELD_DEFINITIONS = List.of( 067 new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, true, null, null) 068 ); 069 } 070 071 /** Creates a new instance of EditEntityAppearanceCommand */ 072 public EditEntityAppearanceCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 protected BaseResult execute() { 078 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 079 var result = CoreResultFactory.getEditEntityAppearanceResult(); 080 var entityRef = spec.getEntityRef(); 081 var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef); 082 083 if(entityInstance != null) { 084 EntityAppearance entityAppearance = null; 085 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 086 087 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 088 var appearanceControl = Session.getModelController(AppearanceControl.class); 089 090 entityAppearance = appearanceControl.getEntityAppearance(entityInstance); 091 092 if(entityAppearance != null) { 093 if(editMode.equals(EditMode.LOCK)) { 094 result.setEntityAppearance(appearanceControl.getEntityAppearanceTransfer(getUserVisit(), entityAppearance)); 095 096 if(lockEntity(basePK)) { 097 var 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 var appearanceName = edit.getAppearanceName(); 114 var appearance = AppearanceLogic.getInstance().getAppearanceByName(this, appearanceName); 115 116 if(!hasExecutionErrors()) { 117 var appearanceControl = Session.getModelController(AppearanceControl.class); 118 119 entityAppearance = appearanceControl.getEntityAppearanceForUpdate(entityInstance); 120 121 if(entityAppearance != null) { 122 if(lockEntityForUpdate(basePK)) { 123 try { 124 var entityAppearanceValue = appearanceControl.getEntityAppearanceValue(entityAppearance); 125 126 entityAppearanceValue.setAppearancePK(appearance.getPrimaryKey()); 127 128 appearanceControl.updateEntityAppearanceFromValue(entityAppearanceValue, getPartyPK()); 129 } finally { 130 unlockEntity(basePK); 131 basePK = null; 132 } 133 } else { 134 addExecutionError(ExecutionErrors.EntityLockStale.name()); 135 } 136 } else { 137 addExecutionError(ExecutionErrors.UnknownEntityAppearance.name(), entityRef); 138 } 139 } 140 } 141 } 142 143 if(basePK != null) { 144 result.setEntityLock(getEntityLockTransfer(basePK)); 145 } 146 147 if(entityAppearance != null) { 148 var appearanceControl = Session.getModelController(AppearanceControl.class); 149 150 result.setEntityAppearance(appearanceControl.getEntityAppearanceTransfer(getUserVisit(), entityAppearance)); 151 } 152 } else { 153 addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef); 154 } 155 156 return result; 157 } 158 159}