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