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