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