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.CreatePrinterForm; 020import com.echothree.model.control.core.server.control.EntityInstanceControl; 021import com.echothree.model.control.printer.common.workflow.PrinterStatusConstants; 022import com.echothree.model.control.printer.server.control.PrinterControl; 023import com.echothree.model.control.workflow.server.control.WorkflowControl; 024import com.echothree.model.data.user.common.pk.UserVisitPK; 025import com.echothree.util.common.command.BaseResult; 026import com.echothree.util.common.message.ExecutionErrors; 027import com.echothree.util.common.validation.FieldDefinition; 028import com.echothree.util.common.validation.FieldType; 029import com.echothree.util.server.control.BaseSimpleCommand; 030import com.echothree.util.server.persistence.Session; 031import java.util.List; 032import javax.enterprise.context.Dependent; 033 034@Dependent 035public class CreatePrinterCommand 036 extends BaseSimpleCommand<CreatePrinterForm> { 037 038 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 039 040 static { 041 FORM_FIELD_DEFINITIONS = List.of( 042 new FieldDefinition("PrinterName", FieldType.ENTITY_NAME, true, null, null), 043 new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null), 044 new FieldDefinition("Priority", FieldType.SIGNED_INTEGER, true, null, null), 045 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 046 ); 047 } 048 049 /** Creates a new instance of CreatePrinterCommand */ 050 public CreatePrinterCommand() { 051 super(null, FORM_FIELD_DEFINITIONS, false); 052 } 053 054 @Override 055 protected BaseResult execute() { 056 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 057 var printerControl = Session.getModelController(PrinterControl.class); 058 var printerName = form.getPrinterName(); 059 var printer = printerControl.getPrinterByName(printerName); 060 061 if(printer == null) { 062 var printerGroupName = form.getPrinterGroupName(); 063 var printerGroup = printerControl.getPrinterGroupByName(printerGroupName); 064 065 if(printerGroup != null) { 066 var workflowControl = Session.getModelController(WorkflowControl.class); 067 var priority = Integer.valueOf(form.getPriority()); 068 var description = form.getDescription(); 069 var createdBy = getPartyPK(); 070 071 printer = printerControl.createPrinter(printerName, printerGroup, priority, createdBy); 072 073 if(description != null) { 074 var language = getPreferredLanguage(); 075 076 printerControl.createPrinterDescription(printer, language, description, createdBy); 077 } 078 079 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(printer.getPrimaryKey()); 080 workflowControl.addEntityToWorkflowUsingNames(null, PrinterStatusConstants.Workflow_PRINTER_STATUS, PrinterStatusConstants.WorkflowEntrance_NEW_PRINTER, entityInstance, null, null, 081 createdBy); 082 } else { 083 addExecutionError(ExecutionErrors.UnknownPrinterGroupName.name(), printerGroupName); 084 } 085 } else { 086 addExecutionError(ExecutionErrors.DuplicatePrinterName.name(), printerName); 087 } 088 089 return null; 090 } 091 092}