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