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 java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
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    @Inject
054    EntityInstanceControl entityInstanceControl;
055
056    @Inject
057    PrinterControl printerControl;
058
059    @Inject
060    WorkflowControl workflowControl;
061
062    @Inject
063    UnitOfMeasureTypeLogic unitOfMeasureTypeLogic;
064
065    /** Creates a new instance of CreatePrinterGroupCommand */
066    public CreatePrinterGroupCommand() {
067        super(null, FORM_FIELD_DEFINITIONS, false);
068    }
069    
070   @Override
071    protected BaseResult execute() {
072       var printerGroupName = form.getPrinterGroupName();
073       var printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
074        
075        if(printerGroup == null) {
076            var keepPrintedJobsTime = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, UomConstants.UnitOfMeasureKindUseType_TIME,
077                    form.getKeepPrintedJobsTime(), form.getKeepPrintedJobsTimeUnitOfMeasureTypeName(),
078                    null, ExecutionErrors.MissingRequiredKeepPrintedJobsTime.name(), null, ExecutionErrors.MissingRequiredKeepPrintedJobsTimeUnitOfMeasureTypeName.name(),
079                    null, ExecutionErrors.UnknownKeepPrintedJobsTimeUnitOfMeasureTypeName.name());
080
081            if(!hasExecutionErrors()) {
082                var isDefault = Boolean.valueOf(form.getIsDefault());
083                var sortOrder = Integer.valueOf(form.getSortOrder());
084                var description = form.getDescription();
085                var createdBy = getPartyPK();
086
087                printerGroup = printerControl.createPrinterGroup(printerGroupName, keepPrintedJobsTime, isDefault, sortOrder, createdBy);
088
089                if(description != null) {
090                    var language = getPreferredLanguage();
091
092                    printerControl.createPrinterGroupDescription(printerGroup, language, description, createdBy);
093                }
094
095                var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(printerGroup.getPrimaryKey());
096                workflowControl.addEntityToWorkflowUsingNames(null, PrinterGroupStatusConstants.Workflow_PRINTER_GROUP_STATUS,
097                        PrinterGroupStatusConstants.WorkflowEntrance_NEW_PRINTER_GROUP, entityInstance, null, null, createdBy);
098            }
099        } else {
100            addExecutionError(ExecutionErrors.DuplicatePrinterGroupName.name(), printerGroupName);
101        }
102        
103        return null;
104    }
105    
106}