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