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.printer.server.command; 018 019import com.echothree.control.user.printer.common.form.CreatePrinterGroupJobForm; 020import com.echothree.control.user.printer.common.result.CreatePrinterGroupJobResult; 021import com.echothree.control.user.printer.common.result.PrinterResultFactory; 022import com.echothree.model.control.core.common.EntityAttributeTypes; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.printer.server.control.PrinterControl; 025import com.echothree.model.control.printer.server.logic.PrinterGroupJobLogic; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.data.core.server.entity.MimeType; 029import com.echothree.model.data.printer.server.entity.PrinterGroup; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.persistence.type.ByteArray; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseSimpleCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class CreatePrinterGroupJobCommand 046 extends BaseSimpleCommand<CreatePrinterGroupJobForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.PrinterGroupJob.name(), SecurityRoles.Create.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null), 062 new FieldDefinition("Copies", FieldType.SIGNED_INTEGER, true, null, null), 063 new FieldDefinition("Priority", FieldType.UNSIGNED_INTEGER, true, 1L, 100L), 064 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L), 065 new FieldDefinition("Clob", FieldType.STRING, false, 1L, null) 066 ); 067 } 068 069 @Inject 070 PrinterControl printerControl; 071 072 @Inject 073 PrinterGroupJobLogic printerGroupJobLogic; 074 075 076 /** Creates a new instance of CreatePrinterGroupJobCommand */ 077 public CreatePrinterGroupJobCommand() { 078 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 079 } 080 081 private void createPrinterGroupJob(final PrinterGroup printerGroup, final MimeType mimeType, final ByteArray blob, final String clob, 082 final CreatePrinterGroupJobResult result) { 083 var copies = Integer.valueOf(form.getCopies()); 084 var priority = Integer.valueOf(form.getPriority()); 085 var description = form.getDescription(); 086 087 var printerGroupJob = printerGroupJobLogic.createPrinterGroupJob(this, printerGroup, copies, priority, mimeType, 088 getPreferredLanguage(), description, blob, clob, getPartyPK()); 089 090 if(!hasExecutionErrors()) { 091 result.setEntityRef(printerGroupJob.getPrimaryKey().getEntityRef()); 092 result.setPrinterGroupJobName(printerGroupJob.getLastDetail().getPrinterGroupJobName()); 093 } 094 } 095 096 @Override 097 protected BaseResult execute() { 098 var result = PrinterResultFactory.getCreatePrinterGroupJobResult(); 099 var printerGroupName = form.getPrinterGroupName(); 100 var printerGroup = printerControl.getPrinterGroupByName(printerGroupName); 101 102 if(printerGroup != null) { 103 var mimeTypeName = form.getMimeTypeName(); 104 var mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName); 105 106 if(mimeType != null) { 107 var entityAttributeType = mimeType.getLastDetail().getEntityAttributeType(); 108 var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName(); 109 110 if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) { 111 var blob = form.getBlob(); 112 113 if(blob != null) { 114 createPrinterGroupJob(printerGroup, mimeType, blob, null, result); 115 } else { 116 addExecutionError(ExecutionErrors.MissingBlob.name()); 117 } 118 } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) { 119 var clob = form.getClob(); 120 121 if(clob != null) { 122 createPrinterGroupJob(printerGroup, mimeType, null, clob, result); 123 } else { 124 addExecutionError(ExecutionErrors.MissingClob.name()); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.UnknownPrinterGroupName.name(), printerGroupName); 134 } 135 136 return result; 137 } 138 139}