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.edit.PartyPrinterGroupUseEdit;
020import com.echothree.control.user.printer.common.edit.PrinterEditFactory;
021import com.echothree.control.user.printer.common.form.EditPartyPrinterGroupUseForm;
022import com.echothree.control.user.printer.common.result.EditPartyPrinterGroupUseResult;
023import com.echothree.control.user.printer.common.result.PrinterResultFactory;
024import com.echothree.control.user.printer.common.spec.PartyPrinterGroupUseSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.printer.server.control.PrinterControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.party.server.entity.Party;
031import com.echothree.model.data.printer.server.entity.PartyPrinterGroupUse;
032import com.echothree.model.data.printer.server.entity.PrinterGroup;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.EditMode;
038import com.echothree.util.server.control.BaseAbstractEditCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
045
046@Dependent
047public class EditPartyPrinterGroupUseCommand
048        extends BaseAbstractEditCommand<PartyPrinterGroupUseSpec, PartyPrinterGroupUseEdit, EditPartyPrinterGroupUseResult, PartyPrinterGroupUse, Party> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
058                        new SecurityRoleDefinition(SecurityRoleGroups.PartyPrinterGroupUse.name(), SecurityRoles.Edit.name())
059                ))
060        ));
061
062        SPEC_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("PrinterGroupUseTypeName", FieldType.ENTITY_NAME, true, null, null)
065        );
066
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null)
069        );
070    }
071
072    @Inject
073    PartyControl partyControl;
074
075    @Inject
076    PrinterControl printerControl;
077
078    
079    /** Creates a new instance of EditPartyPrinterGroupUseCommand */
080    public EditPartyPrinterGroupUseCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083    
084    @Override
085    public EditPartyPrinterGroupUseResult getResult() {
086        return PrinterResultFactory.getEditPartyPrinterGroupUseResult();
087    }
088
089    @Override
090    public PartyPrinterGroupUseEdit getEdit() {
091        return PrinterEditFactory.getPartyPrinterGroupUseEdit();
092    }
093
094    @Override
095    public PartyPrinterGroupUse getEntity(EditPartyPrinterGroupUseResult result) {
096        PartyPrinterGroupUse partyPrinterGroupUse = null;
097        var partyName = spec.getPartyName();
098        Party party;
099
100        if(partyName != null) {
101            party = partyControl.getPartyByName(partyName);
102            
103            if(party == null) {
104                addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
105            }
106        } else {
107            party = getParty();
108        }
109
110        if(!hasExecutionErrors()) {
111            var printerGroupUseTypeName = spec.getPrinterGroupUseTypeName();
112            var printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(printerGroupUseTypeName);
113
114            if(printerGroupUseType != null) {
115                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
116                    partyPrinterGroupUse = printerControl.getPartyPrinterGroupUse(party, printerGroupUseType);
117                } else { // EditMode.UPDATE
118                    partyPrinterGroupUse = printerControl.getPartyPrinterGroupUseForUpdate(party, printerGroupUseType);
119                }
120
121                if(partyPrinterGroupUse == null) {
122                    addExecutionError(ExecutionErrors.UnknownPartyPrinterGroupUse.name(), party.getLastDetail().getPartyName(), printerGroupUseTypeName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownPrinterGroupUseTypeName.name(), printerGroupUseTypeName);
126            }
127        }
128
129        return partyPrinterGroupUse;
130    }
131
132    @Override
133    public Party getLockEntity(PartyPrinterGroupUse partyPrinterGroupUse) {
134        return partyPrinterGroupUse.getParty();
135    }
136
137    @Override
138    public void fillInResult(EditPartyPrinterGroupUseResult result, PartyPrinterGroupUse partyPrinterGroupUse) {
139        result.setPartyPrinterGroupUse(printerControl.getPartyPrinterGroupUseTransfer(getUserVisit(), partyPrinterGroupUse));
140    }
141
142    @Override
143    public void doLock(PartyPrinterGroupUseEdit edit, PartyPrinterGroupUse partyPrinterGroupUse) {
144        edit.setPrinterGroupName(partyPrinterGroupUse.getPrinterGroup().getLastDetail().getPrinterGroupName());
145    }
146
147    PrinterGroup printerGroup;
148
149    @Override
150    public void canUpdate(PartyPrinterGroupUse partyPrinterGroupUse) {
151        var printerGroupName = edit.getPrinterGroupName();
152
153        printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
154
155        if(printerGroup == null) {
156            addExecutionError(ExecutionErrors.DuplicatePrinterGroupName.name(), printerGroupName);
157        }
158    }
159
160    @Override
161    public void doUpdate(PartyPrinterGroupUse partyPrinterGroupUse) {
162        var partyPrinterGroupUseValue = printerControl.getPartyPrinterGroupUseValue(partyPrinterGroupUse);
163
164        partyPrinterGroupUseValue.setPrinterGroupPK(printerGroup.getPrimaryKey());
165
166        printerControl.updatePartyPrinterGroupUseFromValue(partyPrinterGroupUseValue, getPartyPK());
167    }
168    
169}