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.scale.server.command; 018 019import com.echothree.control.user.scale.common.edit.PartyScaleUseEdit; 020import com.echothree.control.user.scale.common.edit.ScaleEditFactory; 021import com.echothree.control.user.scale.common.form.EditPartyScaleUseForm; 022import com.echothree.control.user.scale.common.result.EditPartyScaleUseResult; 023import com.echothree.control.user.scale.common.result.ScaleResultFactory; 024import com.echothree.control.user.scale.common.spec.PartyScaleUseSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.scale.server.control.ScaleControl; 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.scale.server.entity.PartyScaleUse; 032import com.echothree.model.data.scale.server.entity.Scale; 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 com.echothree.util.server.persistence.Session; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045 046@Dependent 047public class EditPartyScaleUseCommand 048 extends BaseAbstractEditCommand<PartyScaleUseSpec, PartyScaleUseEdit, EditPartyScaleUseResult, PartyScaleUse, 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.PartyScaleUse.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("ScaleUseTypeName", FieldType.ENTITY_NAME, true, null, null) 065 ); 066 067 EDIT_FIELD_DEFINITIONS = List.of( 068 new FieldDefinition("ScaleName", FieldType.ENTITY_NAME, true, null, null) 069 ); 070 } 071 072 /** Creates a new instance of EditPartyScaleUseCommand */ 073 public EditPartyScaleUseCommand() { 074 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 public EditPartyScaleUseResult getResult() { 079 return ScaleResultFactory.getEditPartyScaleUseResult(); 080 } 081 082 @Override 083 public PartyScaleUseEdit getEdit() { 084 return ScaleEditFactory.getPartyScaleUseEdit(); 085 } 086 087 @Override 088 public PartyScaleUse getEntity(EditPartyScaleUseResult result) { 089 PartyScaleUse partyScaleUse = null; 090 var partyName = spec.getPartyName(); 091 Party party; 092 093 if(partyName != null) { 094 var partyControl = Session.getModelController(PartyControl.class); 095 096 party = partyControl.getPartyByName(partyName); 097 098 if(party == null) { 099 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 100 } 101 } else { 102 party = getParty(); 103 } 104 105 if(!hasExecutionErrors()) { 106 var scaleControl = Session.getModelController(ScaleControl.class); 107 var scaleUseTypeName = spec.getScaleUseTypeName(); 108 var scaleUseType = scaleControl.getScaleUseTypeByName(scaleUseTypeName); 109 110 if(scaleUseType != null) { 111 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 112 partyScaleUse = scaleControl.getPartyScaleUse(party, scaleUseType); 113 } else { // EditMode.UPDATE 114 partyScaleUse = scaleControl.getPartyScaleUseForUpdate(party, scaleUseType); 115 } 116 117 if(partyScaleUse == null) { 118 addExecutionError(ExecutionErrors.UnknownPartyScaleUse.name(), party.getLastDetail().getPartyName(), scaleUseTypeName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.UnknownScaleUseTypeName.name(), scaleUseTypeName); 122 } 123 } 124 125 return partyScaleUse; 126 } 127 128 @Override 129 public Party getLockEntity(PartyScaleUse partyScaleUse) { 130 return partyScaleUse.getParty(); 131 } 132 133 @Override 134 public void fillInResult(EditPartyScaleUseResult result, PartyScaleUse partyScaleUse) { 135 var scaleControl = Session.getModelController(ScaleControl.class); 136 137 result.setPartyScaleUse(scaleControl.getPartyScaleUseTransfer(getUserVisit(), partyScaleUse)); 138 } 139 140 @Override 141 public void doLock(PartyScaleUseEdit edit, PartyScaleUse partyScaleUse) { 142 edit.setScaleName(partyScaleUse.getScale().getLastDetail().getScaleName()); 143 } 144 145 Scale scale; 146 147 @Override 148 public void canUpdate(PartyScaleUse partyScaleUse) { 149 var scaleControl = Session.getModelController(ScaleControl.class); 150 var scaleName = edit.getScaleName(); 151 152 scale = scaleControl.getScaleByName(scaleName); 153 154 if(scale == null) { 155 addExecutionError(ExecutionErrors.DuplicateScaleName.name(), scaleName); 156 } 157 } 158 159 @Override 160 public void doUpdate(PartyScaleUse partyScaleUse) { 161 var scaleControl = Session.getModelController(ScaleControl.class); 162 var partyScaleUseValue = scaleControl.getPartyScaleUseValue(partyScaleUse); 163 164 partyScaleUseValue.setScalePK(scale.getPrimaryKey()); 165 166 scaleControl.updatePartyScaleUseFromValue(partyScaleUseValue, getPartyPK()); 167 } 168 169}