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