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.EntityClobAttributeEdit;
021import com.echothree.control.user.core.common.form.EditEntityClobAttributeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.spec.EntityClobAttributeSpec;
024import com.echothree.model.control.core.common.EntityAttributeTypes;
025import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
026import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
027import com.echothree.model.control.core.server.logic.MimeTypeLogic;
028import com.echothree.model.control.party.common.PartyTypes;
029import com.echothree.model.control.party.server.logic.LanguageLogic;
030import com.echothree.model.data.core.server.entity.EntityClobAttribute;
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.persistence.PersistenceUtils;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditEntityClobAttributeCommand
046        extends BaseEditCommand<EntityClobAttributeSpec, EntityClobAttributeEdit> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
056        ));
057
058        SPEC_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
060                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
061                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null),
063                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("LanguageUuid", FieldType.UUID, false, null, null)
065                );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("ClobAttribute", FieldType.STRING, true, 1L, null),
069                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null)
070                );
071    }
072    
073    /** Creates a new instance of EditEntityClobAttributeCommand */
074    public EditEntityClobAttributeCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    protected BaseResult execute() {
080        var result = CoreResultFactory.getEditEntityClobAttributeResult();
081        var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, spec);
082        var language = LanguageLogic.getInstance().getLanguage(this, spec, spec);
083        var entityAttribute = EntityAttributeLogic.getInstance().getEntityAttribute(this, entityInstance, spec, spec,
084                EntityAttributeTypes.CLOB);
085
086        if(!hasExecutionErrors()) {
087            EntityClobAttribute entityClobAttribute = null;
088            var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
089
090            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
091                entityClobAttribute = coreControl.getEntityClobAttribute(entityAttribute, entityInstance, language);
092
093                if(entityClobAttribute != null) {
094                    if(editMode.equals(EditMode.LOCK)) {
095                        result.setEntityClobAttribute(coreControl.getEntityClobAttributeTransfer(getUserVisit(), entityClobAttribute, entityInstance));
096
097                        if(lockEntity(basePK)) {
098                            var edit = CoreEditFactory.getEntityClobAttributeEdit();
099
100                            result.setEdit(edit);
101                            edit.setClobAttribute(entityClobAttribute.getClobAttribute());
102                            edit.setMimeTypeName(entityClobAttribute.getMimeType().getLastDetail().getMimeTypeName());
103                        } else {
104                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
105                        }
106                    } else { // EditMode.ABANDON
107                        unlockEntity(basePK);
108                        basePK = null;
109                    }
110                } else {
111                    var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
112
113                    addExecutionError(ExecutionErrors.UnknownEntityClobAttribute.name(), basePK.getEntityRef(),
114                            entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
115                            entityTypeDetail.getEntityTypeName(), entityAttribute.getLastDetail().getEntityAttributeName(),
116                            language.getLanguageIsoName());
117                }
118            } else if(editMode.equals(EditMode.UPDATE)) {
119                entityClobAttribute = coreControl.getEntityClobAttributeForUpdate(entityAttribute, entityInstance, language);
120
121                if(entityClobAttribute != null) {
122                    var mimeType = MimeTypeLogic.getInstance().getMimeTypeByName(this, edit.getMimeTypeName());
123
124                    if(!hasExecutionErrors()) {
125                        if(mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName().equals(EntityAttributeTypes.CLOB.name())) {
126                            if(lockEntityForUpdate(basePK)) {
127                                try {
128                                    var entityClobAttributeValue = coreControl.getEntityClobAttributeValueForUpdate(entityClobAttribute);
129
130                                    entityClobAttributeValue.setClobAttribute(edit.getClobAttribute());
131                                    entityClobAttributeValue.setMimeTypePK(mimeType.getPrimaryKey());
132
133                                    coreControl.updateEntityClobAttributeFromValue(entityClobAttributeValue, getPartyPK());
134                                } finally {
135                                    unlockEntity(basePK);
136                                    basePK = null;
137                                }
138                            } else {
139                                addExecutionError(ExecutionErrors.EntityLockStale.name());
140                            }
141                        } else {
142                            addExecutionError(ExecutionErrors.InvalidMimeType.name(), mimeType.getLastDetail().getMimeTypeName());
143                        }
144                    }
145                } else {
146                    var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
147
148                    addExecutionError(ExecutionErrors.UnknownEntityClobAttribute.name(), basePK.getEntityRef(),
149                            entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
150                            entityTypeDetail.getEntityTypeName(), entityAttribute.getLastDetail().getEntityAttributeName(),
151                            language.getLanguageIsoName());
152                }
153            }
154
155            if(basePK != null) {
156                result.setEntityLock(getEntityLockTransfer(basePK));
157            }
158
159            if(entityClobAttribute != null) {
160                result.setEntityClobAttribute(coreControl.getEntityClobAttributeTransfer(getUserVisit(), entityClobAttribute, entityInstance));
161            }
162        }
163
164        return result;
165    }
166    
167}