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.EntityTimeAttributeEdit;
021import com.echothree.control.user.core.common.form.EditEntityTimeAttributeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.spec.EntityTimeAttributeSpec;
024import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
025import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.data.core.server.entity.EntityTimeAttribute;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.persistence.PersistenceUtils;
038import com.echothree.util.server.string.DateUtils;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditEntityTimeAttributeCommand
045        extends BaseEditCommand<EntityTimeAttributeSpec, EntityTimeAttributeEdit> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
055        ));
056
057        SPEC_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
059                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
060                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null)
062        );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("TimeAttribute", FieldType.DATE_TIME, true, null, null)
066        );
067    }
068
069    @Inject
070    EntityAttributeLogic entityAttributeLogic;
071
072    @Inject
073    EntityInstanceLogic entityInstanceLogic;
074
075    
076    /** Creates a new instance of EditEntityTimeAttributeCommand */
077    public EditEntityTimeAttributeCommand() {
078        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080    
081    @Override
082    protected BaseResult execute() {
083        var result = CoreResultFactory.getEditEntityTimeAttributeResult();
084        var parameterCount = entityInstanceLogic.countPossibleEntitySpecs(spec);
085
086        if(parameterCount == 1) {
087            var entityInstance = entityInstanceLogic.getEntityInstance(this, spec);
088
089            if(!hasExecutionErrors()) {
090                var entityAttributeName = spec.getEntityAttributeName();
091                var entityAttributeUuid = spec.getEntityAttributeUuid();
092
093                parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUuid == null ? 0 : 1);
094
095                if(parameterCount == 1) {
096                    var entityAttribute = entityAttributeName == null ?
097                            entityAttributeLogic.getEntityAttributeByUuid(this, entityAttributeUuid) :
098                            entityAttributeLogic.getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName);
099
100                    if(!hasExecutionErrors()) {
101                        if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) {
102                            EntityTimeAttribute entityTimeAttribute = null;
103                            var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
104
105                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
106                                entityTimeAttribute = coreControl.getEntityTimeAttribute(entityAttribute, entityInstance);
107
108                                if(entityTimeAttribute != null) {
109                                    if(editMode.equals(EditMode.LOCK)) {
110                                        result.setEntityTimeAttribute(coreControl.getEntityTimeAttributeTransfer(getUserVisit(),
111                                                entityTimeAttribute, entityInstance));
112
113                                        if(lockEntity(basePK)) {
114                                            var edit = CoreEditFactory.getEntityTimeAttributeEdit();
115
116                                            result.setEdit(edit);
117                                            edit.setTimeAttribute(DateUtils.getInstance().formatTypicalDateTime(getUserVisit(), entityTimeAttribute.getTimeAttribute()));
118                                        } else {
119                                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
120                                        }
121                                    } else { // EditMode.ABANDON
122                                        unlockEntity(basePK);
123                                        basePK = null;
124                                    }
125                                } else {
126                                    addExecutionError(ExecutionErrors.UnknownEntityTimeAttribute.name(),
127                                            entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
128                                }
129                            } else if(editMode.equals(EditMode.UPDATE)) {
130                                entityTimeAttribute = coreControl.getEntityTimeAttributeForUpdate(entityAttribute, entityInstance);
131
132                                if(entityTimeAttribute != null) {
133                                    if(lockEntityForUpdate(basePK)) {
134                                        try {
135                                            var entityTimeAttributeValue = coreControl.getEntityTimeAttributeValueForUpdate(entityTimeAttribute);
136
137                                            entityTimeAttributeValue.setTimeAttribute(Long.valueOf(edit.getTimeAttribute()));
138
139                                            coreControl.updateEntityTimeAttributeFromValue(entityTimeAttributeValue, getPartyPK());
140                                        } finally {
141                                            unlockEntity(basePK);
142                                            basePK = null;
143                                        }
144                                    } else {
145                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
146                                    }
147                                } else {
148                                    addExecutionError(ExecutionErrors.UnknownEntityTimeAttribute.name(),
149                                            entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
150                                }
151                            }
152
153                            if(basePK != null) {
154                                result.setEntityLock(getEntityLockTransfer(basePK));
155                            }
156
157                            if(entityTimeAttribute != null) {
158                                result.setEntityTimeAttribute(coreControl.getEntityTimeAttributeTransfer(getUserVisit(), entityTimeAttribute, entityInstance));
159                            }
160                        } else {
161                            addExecutionError(ExecutionErrors.MismatchedEntityType.name());
162                        }
163                    }
164                } else {
165                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
166                }
167            }
168        } else {
169            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
170        }
171
172        return result;
173    }
174    
175}