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 java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
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    @Inject
072    AppearanceControl appearanceControl;
073
074    @Inject
075    EntityInstanceControl entityInstanceControl;
076
077    @Inject
078    AppearanceLogic appearanceLogic;
079
080    
081    /** Creates a new instance of EditEntityAppearanceCommand */
082    public EditEntityAppearanceCommand() {
083        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085    
086    @Override
087    protected BaseResult execute() {
088        var result = CoreResultFactory.getEditEntityAppearanceResult();
089        var entityRef = spec.getEntityRef();
090        var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef);
091
092        if(entityInstance != null) {
093            EntityAppearance entityAppearance = null;
094            var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
095
096            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
097                entityAppearance = appearanceControl.getEntityAppearance(entityInstance);
098
099                if(entityAppearance != null) {
100                    if(editMode.equals(EditMode.LOCK)) {
101                        result.setEntityAppearance(appearanceControl.getEntityAppearanceTransfer(getUserVisit(), entityAppearance));
102
103                        if(lockEntity(basePK)) {
104                            var edit = CoreEditFactory.getEntityAppearanceEdit();
105
106                            result.setEdit(edit);
107                            edit.setAppearanceName(entityAppearance.getAppearance().getLastDetail().getAppearanceName());
108                        } else {
109                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
110                        }
111                    } else { // EditMode.ABANDON
112                        unlockEntity(basePK);
113                        basePK = null;
114                    }
115                } else {
116                    addExecutionError(ExecutionErrors.UnknownEntityAppearance.name(), entityRef);
117                }
118            } else {
119                if(editMode.equals(EditMode.UPDATE)) {
120                    var appearanceName = edit.getAppearanceName();
121                    var appearance = appearanceLogic.getAppearanceByName(this, appearanceName);
122
123                    if(!hasExecutionErrors()) {
124                        entityAppearance = appearanceControl.getEntityAppearanceForUpdate(entityInstance);
125
126                        if(entityAppearance != null) {
127                            if(lockEntityForUpdate(basePK)) {
128                                try {
129                                    var entityAppearanceValue = appearanceControl.getEntityAppearanceValue(entityAppearance);
130
131                                    entityAppearanceValue.setAppearancePK(appearance.getPrimaryKey());
132
133                                    appearanceControl.updateEntityAppearanceFromValue(entityAppearanceValue, getPartyPK());
134                                } finally {
135                                    unlockEntity(basePK);
136                                    basePK = null;
137                                }
138                            } else {
139                                addExecutionError(ExecutionErrors.EntityLockStale.name());
140                            }
141                        } else {
142                            addExecutionError(ExecutionErrors.UnknownEntityAppearance.name(), entityRef);
143                        }
144                    }
145                }
146            }
147
148            if(basePK != null) {
149                result.setEntityLock(getEntityLockTransfer(basePK));
150            }
151
152            if(entityAppearance != null) {
153                result.setEntityAppearance(appearanceControl.getEntityAppearanceTransfer(getUserVisit(), entityAppearance));
154            }
155        } else {
156            addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
157        }
158
159        return result;
160    }
161    
162}