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