001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.PrinterEdit; 020import com.echothree.control.user.printer.common.edit.PrinterEditFactory; 021import com.echothree.control.user.printer.common.form.EditPrinterForm; 022import com.echothree.control.user.printer.common.result.EditPrinterResult; 023import com.echothree.control.user.printer.common.result.PrinterResultFactory; 024import com.echothree.control.user.printer.common.spec.PrinterSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.printer.server.control.PrinterControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.printer.server.entity.Printer; 030import com.echothree.model.data.printer.server.entity.PrinterDescription; 031import com.echothree.model.data.printer.server.entity.PrinterDetail; 032import com.echothree.model.data.printer.server.entity.PrinterGroup; 033import com.echothree.model.data.printer.server.value.PrinterDescriptionValue; 034import com.echothree.model.data.printer.server.value.PrinterDetailValue; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.common.command.EditMode; 040import com.echothree.util.server.control.BaseAbstractEditCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.Session; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class EditPrinterCommand 050 extends BaseAbstractEditCommand<PrinterSpec, PrinterEdit, EditPrinterResult, Printer, Printer> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 054 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 060 new SecurityRoleDefinition(SecurityRoleGroups.Printer.name(), SecurityRoles.Edit.name()) 061 ))) 062 ))); 063 064 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 065 new FieldDefinition("PrinterName", FieldType.ENTITY_NAME, true, null, null) 066 )); 067 068 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 069 new FieldDefinition("PrinterName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("Priority", FieldType.SIGNED_INTEGER, true, null, null), 071 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 072 )); 073 } 074 075 /** Creates a new instance of EditPrinterCommand */ 076 public EditPrinterCommand(UserVisitPK userVisitPK, EditPrinterForm form) { 077 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 public EditPrinterResult getResult() { 082 return PrinterResultFactory.getEditPrinterResult(); 083 } 084 085 @Override 086 public PrinterEdit getEdit() { 087 return PrinterEditFactory.getPrinterEdit(); 088 } 089 090 @Override 091 public Printer getEntity(EditPrinterResult result) { 092 var printerControl = Session.getModelController(PrinterControl.class); 093 Printer printer = null; 094 String printerName = spec.getPrinterName(); 095 096 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 097 printer = printerControl.getPrinterByName(printerName); 098 } else { // EditMode.UPDATE 099 printer = printerControl.getPrinterByNameForUpdate(printerName); 100 } 101 102 if(printer != null) { 103 result.setPrinter(printerControl.getPrinterTransfer(getUserVisit(), printer)); 104 } else { 105 addExecutionError(ExecutionErrors.UnknownPrinterName.name(), printerName); 106 } 107 108 return printer; 109 } 110 111 @Override 112 public Printer getLockEntity(Printer printer) { 113 return printer; 114 } 115 116 @Override 117 public void fillInResult(EditPrinterResult result, Printer printer) { 118 var printerControl = Session.getModelController(PrinterControl.class); 119 120 result.setPrinter(printerControl.getPrinterTransfer(getUserVisit(), printer)); 121 } 122 123 @Override 124 public void doLock(PrinterEdit edit, Printer printer) { 125 var printerControl = Session.getModelController(PrinterControl.class); 126 PrinterDescription printerDescription = printerControl.getPrinterDescription(printer, getPreferredLanguage()); 127 PrinterDetail printerDetail = printer.getLastDetail(); 128 129 edit.setPrinterName(printerDetail.getPrinterName()); 130 edit.setPrinterGroupName(printerDetail.getPrinterGroup().getLastDetail().getPrinterGroupName()); 131 edit.setPriority(printerDetail.getPriority().toString()); 132 133 if(printerDescription != null) { 134 edit.setDescription(printerDescription.getDescription()); 135 } 136 } 137 138 PrinterGroup printerGroup; 139 140 @Override 141 public void canUpdate(Printer printer) { 142 var printerControl = Session.getModelController(PrinterControl.class); 143 String printerName = edit.getPrinterName(); 144 Printer duplicatePrinter = printerControl.getPrinterByName(printerName); 145 146 if(duplicatePrinter != null && !printer.equals(duplicatePrinter)) { 147 addExecutionError(ExecutionErrors.DuplicatePrinterName.name(), printerName); 148 } else { 149 String printerGroupName = edit.getPrinterGroupName(); 150 151 printerGroup = printerControl.getPrinterGroupByName(printerGroupName); 152 153 if(printerGroup == null) { 154 addExecutionError(ExecutionErrors.DuplicatePrinterGroupName.name(), printerGroupName); 155 } 156 } 157 } 158 159 @Override 160 public void doUpdate(Printer printer) { 161 var printerControl = Session.getModelController(PrinterControl.class); 162 var partyPK = getPartyPK(); 163 PrinterDetailValue printerDetailValue = printerControl.getPrinterDetailValueForUpdate(printer); 164 PrinterDescription printerDescription = printerControl.getPrinterDescriptionForUpdate(printer, getPreferredLanguage()); 165 String description = edit.getDescription(); 166 167 printerDetailValue.setPrinterName(edit.getPrinterName()); 168 printerDetailValue.setPrinterGroupPK(printerGroup.getPrimaryKey()); 169 printerDetailValue.setPriority(Integer.valueOf(edit.getPriority())); 170 171 printerControl.updatePrinterFromValue(printerDetailValue, partyPK); 172 173 if(printerDescription == null && description != null) { 174 printerControl.createPrinterDescription(printer, getPreferredLanguage(), description, partyPK); 175 } else if(printerDescription != null && description == null) { 176 printerControl.deletePrinterDescription(printerDescription, partyPK); 177 } else if(printerDescription != null && description != null) { 178 PrinterDescriptionValue printerDescriptionValue = printerControl.getPrinterDescriptionValue(printerDescription); 179 180 printerDescriptionValue.setDescription(description); 181 printerControl.updatePrinterDescriptionFromValue(printerDescriptionValue, partyPK); 182 } 183 } 184 185}