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.form.CreateCacheEntryForm;
020import com.echothree.model.control.core.common.EntityAttributeTypes;
021import com.echothree.model.control.core.server.control.CacheEntryControl;
022import com.echothree.model.control.uom.common.UomConstants;
023import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic;
024import com.echothree.util.common.command.BaseResult;
025import com.echothree.util.common.exception.PersistenceDatabaseException;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.server.control.BaseSimpleCommand;
030import com.echothree.util.server.persistence.PersistenceUtils;
031import java.util.List;
032import javax.enterprise.context.Dependent;
033import javax.inject.Inject;
034
035@Dependent
036public class CreateCacheEntryCommand
037        extends BaseSimpleCommand<CreateCacheEntryForm> {
038    
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
043                new FieldDefinition("CacheEntryKey", FieldType.STRING, true, 1L, 200L),
044                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null),
045                new FieldDefinition("ValidForTime", FieldType.UNSIGNED_LONG, false, null, null),
046                new FieldDefinition("ValidForTimeUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("Clob", FieldType.STRING, false, 1L, null)
048        );
049    }
050
051    @Inject
052    CacheEntryControl cacheEntryControl;
053
054    @Inject
055    UnitOfMeasureTypeLogic unitOfMeasureTypeLogic;
056
057    
058    /** Creates a new instance of CreateCacheEntryCommand */
059    public CreateCacheEntryCommand() {
060        super(null, FORM_FIELD_DEFINITIONS, false);
061    }
062    
063    @Override
064    protected BaseResult execute() {
065        var cacheEntryKey = form.getCacheEntryKey();
066        var cacheEntry = cacheEntryControl.getCacheEntryByCacheEntryKey(cacheEntryKey);
067
068        if(cacheEntry == null) {
069            var mimeTypeName = form.getMimeTypeName();
070            var mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName);
071
072            if(mimeType != null) {
073                var validForTime = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, UomConstants.UnitOfMeasureKindUseType_TIME,
074                        form.getValidForTime(), form.getValidForTimeUnitOfMeasureTypeName(),
075                        null, ExecutionErrors.MissingRequiredValidForTime.name(), null, ExecutionErrors.MissingRequiredValidForTimeUnitOfMeasureTypeName.name(),
076                        null, ExecutionErrors.UnknownValidForTimeUnitOfMeasureTypeName.name());
077
078                if(!hasExecutionErrors()) {
079                    var entityAttributeTypeName = mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
080                    var entityRefs = form.getEntityRefs();
081
082                    try {
083                        if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) {
084                            var clob = form.getClob();
085
086                            if(clob != null) {
087                                cacheEntryControl.createCacheEntry(cacheEntryKey, mimeType, session.getStartTime(),
088                                        validForTime == null ? null : session.getStartTime() + validForTime, clob, null, entityRefs);
089                            } else {
090                                addExecutionError(ExecutionErrors.MissingClob.name());
091                            }
092                        } else if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) {
093                            var blob = form.getBlob();
094
095                            if(blob != null) {
096                                cacheEntryControl.createCacheEntry(cacheEntryKey, mimeType, session.getStartTime(),
097                                        validForTime == null ? null : session.getStartTime() + validForTime, null, blob, entityRefs);
098                            } else {
099                                addExecutionError(ExecutionErrors.MissingBlob.name());
100                            }
101                        } else {
102                            addExecutionError(ExecutionErrors.InvalidMimeType.name(), mimeTypeName);
103                        }
104                    } catch(PersistenceDatabaseException pde) {
105                        if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) {
106                            // Duplicate key in index, add this as a regular error, and continue. Tested w/ MySQL only.
107                            addExecutionError(ExecutionErrors.DuplicateCacheEntryKey.name(), cacheEntryKey);
108                            pde = null;
109                        }
110
111                        if(pde != null) {
112                            throw pde;
113                        }
114                    }
115                }
116            } else {
117                addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName);
118            }
119        } else {
120            addExecutionError(ExecutionErrors.DuplicateCacheEntryKey.name(), cacheEntryKey);
121        }
122        
123        return null;
124    }
125    
126}