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