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.CreatePrinterGroupJobForm;
020import com.echothree.control.user.printer.common.result.CreatePrinterGroupJobResult;
021import com.echothree.control.user.printer.common.result.PrinterResultFactory;
022import com.echothree.model.control.core.common.EntityAttributeTypes;
023import com.echothree.model.control.core.server.control.MimeTypeControl;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.printer.server.control.PrinterControl;
026import com.echothree.model.control.printer.server.logic.PrinterGroupJobLogic;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.core.server.entity.MimeType;
030import com.echothree.model.data.printer.server.entity.PrinterGroup;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.persistence.type.ByteArray;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044
045@Dependent
046public class CreatePrinterGroupJobCommand
047        extends BaseSimpleCommand<CreatePrinterGroupJobForm> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                    new SecurityRoleDefinition(SecurityRoleGroups.PrinterGroupJob.name(), SecurityRoles.Create.name())
057                    ))
058                ));
059        
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null),
063                new FieldDefinition("Copies", FieldType.SIGNED_INTEGER, true, null, null),
064                new FieldDefinition("Priority", FieldType.UNSIGNED_INTEGER, true, 1L, 100L),
065                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L),
066                new FieldDefinition("Clob", FieldType.STRING, false, 1L, null)
067                );
068    }
069    
070    /** Creates a new instance of CreatePrinterGroupJobCommand */
071    public CreatePrinterGroupJobCommand() {
072        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
073    }
074
075    private void createPrinterGroupJob(final PrinterGroup printerGroup, final MimeType mimeType, final ByteArray blob, final String clob,
076            final CreatePrinterGroupJobResult result) {
077        var copies = Integer.valueOf(form.getCopies());
078        var priority = Integer.valueOf(form.getPriority());
079        var description = form.getDescription();
080
081        var printerGroupJob = PrinterGroupJobLogic.getInstance().createPrinterGroupJob(this, printerGroup, copies, priority, mimeType,
082                getPreferredLanguage(), description, blob, clob, getPartyPK());
083
084        if(!hasExecutionErrors()) {
085            result.setEntityRef(printerGroupJob.getPrimaryKey().getEntityRef());
086            result.setPrinterGroupJobName(printerGroupJob.getLastDetail().getPrinterGroupJobName());
087        }
088    }
089
090    @Override
091    protected BaseResult execute() {
092        var printerControl = Session.getModelController(PrinterControl.class);
093        var result = PrinterResultFactory.getCreatePrinterGroupJobResult();
094        var printerGroupName = form.getPrinterGroupName();
095        var printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
096
097        if(printerGroup != null) {
098            var mimeTypeControl = Session.getModelController(MimeTypeControl.class);
099            var mimeTypeName = form.getMimeTypeName();
100            var mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName);
101
102            if(mimeType != null) {
103                var entityAttributeType = mimeType.getLastDetail().getEntityAttributeType();
104                var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName();
105
106                if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) {
107                    var blob = form.getBlob();
108
109                    if(blob != null) {
110                        createPrinterGroupJob(printerGroup, mimeType, blob, null, result);
111                    } else {
112                        addExecutionError(ExecutionErrors.MissingBlob.name());
113                    }
114                } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) {
115                    var clob = form.getClob();
116
117                    if(clob != null) {
118                        createPrinterGroupJob(printerGroup, mimeType, null, clob, result);
119                    } else {
120                        addExecutionError(ExecutionErrors.MissingClob.name());
121                    }
122                } else {
123                    addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName);
124                }
125            } else {
126                addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName);
127            }
128        } else {
129            addExecutionError(ExecutionErrors.UnknownPrinterGroupName.name(), printerGroupName);
130        }
131
132        return result;
133    }
134    
135}