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.printer.server.command; 018 019import com.echothree.control.user.printer.common.form.CreatePrinterGroupForm; 020import com.echothree.model.control.printer.server.control.PrinterControl; 021import com.echothree.model.control.uom.common.UomConstants; 022import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic; 023import com.echothree.model.control.printer.common.workflow.PrinterGroupStatusConstants; 024import com.echothree.model.control.workflow.server.control.WorkflowControl; 025import com.echothree.model.data.core.server.entity.EntityInstance; 026import com.echothree.model.data.party.common.pk.PartyPK; 027import com.echothree.model.data.party.server.entity.Language; 028import com.echothree.model.data.printer.server.entity.PrinterGroup; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.server.control.BaseSimpleCommand; 035import com.echothree.util.server.persistence.Session; 036import java.util.Arrays; 037import java.util.Collections; 038import java.util.List; 039 040public class CreatePrinterGroupCommand 041 extends BaseSimpleCommand<CreatePrinterGroupForm> { 042 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 047 new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("KeepPrintedJobsTime", FieldType.UNSIGNED_LONG, false, null, null), 049 new FieldDefinition("KeepPrintedJobsTimeUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 051 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 052 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 053 )); 054 } 055 056 /** Creates a new instance of CreatePrinterGroupCommand */ 057 public CreatePrinterGroupCommand(UserVisitPK userVisitPK, CreatePrinterGroupForm form) { 058 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 059 } 060 061 @Override 062 protected BaseResult execute() { 063 var printerControl = Session.getModelController(PrinterControl.class); 064 String printerGroupName = form.getPrinterGroupName(); 065 PrinterGroup printerGroup = printerControl.getPrinterGroupByName(printerGroupName); 066 067 if(printerGroup == null) { 068 UnitOfMeasureTypeLogic unitOfMeasureTypeLogic = UnitOfMeasureTypeLogic.getInstance(); 069 Long keepPrintedJobsTime = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, UomConstants.UnitOfMeasureKindUseType_TIME, 070 form.getKeepPrintedJobsTime(), form.getKeepPrintedJobsTimeUnitOfMeasureTypeName(), 071 null, ExecutionErrors.MissingRequiredKeepPrintedJobsTime.name(), null, ExecutionErrors.MissingRequiredKeepPrintedJobsTimeUnitOfMeasureTypeName.name(), 072 null, ExecutionErrors.UnknownKeepPrintedJobsTimeUnitOfMeasureTypeName.name()); 073 074 if(!hasExecutionErrors()) { 075 var coreControl = getCoreControl(); 076 var workflowControl = Session.getModelController(WorkflowControl.class); 077 var isDefault = Boolean.valueOf(form.getIsDefault()); 078 var sortOrder = Integer.valueOf(form.getSortOrder()); 079 var description = form.getDescription(); 080 PartyPK createdBy = getPartyPK(); 081 082 printerGroup = printerControl.createPrinterGroup(printerGroupName, keepPrintedJobsTime, isDefault, sortOrder, createdBy); 083 084 if(description != null) { 085 Language language = getPreferredLanguage(); 086 087 printerControl.createPrinterGroupDescription(printerGroup, language, description, createdBy); 088 } 089 090 EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(printerGroup.getPrimaryKey()); 091 workflowControl.addEntityToWorkflowUsingNames(null, PrinterGroupStatusConstants.Workflow_PRINTER_GROUP_STATUS, 092 PrinterGroupStatusConstants.WorkflowEntrance_NEW_PRINTER_GROUP, entityInstance, null, null, createdBy); 093 } 094 } else { 095 addExecutionError(ExecutionErrors.DuplicatePrinterGroupName.name(), printerGroupName); 096 } 097 098 return null; 099 } 100 101}