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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.ProtocolEdit; 021import com.echothree.control.user.core.common.form.EditProtocolForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditProtocolResult; 024import com.echothree.control.user.core.common.spec.ProtocolSpec; 025import com.echothree.model.control.core.server.control.ServerControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.core.server.entity.Protocol; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.EditMode; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseAbstractEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042 043@Dependent 044public class EditProtocolCommand 045 extends BaseAbstractEditCommand<ProtocolSpec, ProtocolEdit, EditProtocolResult, Protocol, Protocol> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.Protocol.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("ProtocolName", FieldType.ENTITY_NAME, true, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("ProtocolName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 ); 069 } 070 071 /** Creates a new instance of EditProtocolCommand */ 072 public EditProtocolCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 public EditProtocolResult getResult() { 078 return CoreResultFactory.getEditProtocolResult(); 079 } 080 081 @Override 082 public ProtocolEdit getEdit() { 083 return CoreEditFactory.getProtocolEdit(); 084 } 085 086 @Override 087 public Protocol getEntity(EditProtocolResult result) { 088 var serverControl = Session.getModelController(ServerControl.class); 089 Protocol protocol; 090 var protocolName = spec.getProtocolName(); 091 092 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 093 protocol = serverControl.getProtocolByName(protocolName); 094 } else { // EditMode.UPDATE 095 protocol = serverControl.getProtocolByNameForUpdate(protocolName); 096 } 097 098 if(protocol != null) { 099 result.setProtocol(serverControl.getProtocolTransfer(getUserVisit(), protocol)); 100 } else { 101 addExecutionError(ExecutionErrors.UnknownProtocolName.name(), protocolName); 102 } 103 104 return protocol; 105 } 106 107 @Override 108 public Protocol getLockEntity(Protocol protocol) { 109 return protocol; 110 } 111 112 @Override 113 public void fillInResult(EditProtocolResult result, Protocol protocol) { 114 var serverControl = Session.getModelController(ServerControl.class); 115 116 result.setProtocol(serverControl.getProtocolTransfer(getUserVisit(), protocol)); 117 } 118 119 @Override 120 public void doLock(ProtocolEdit edit, Protocol protocol) { 121 var serverControl = Session.getModelController(ServerControl.class); 122 var protocolDescription = serverControl.getProtocolDescription(protocol, getPreferredLanguage()); 123 var protocolDetail = protocol.getLastDetail(); 124 125 edit.setProtocolName(protocolDetail.getProtocolName()); 126 edit.setIsDefault(protocolDetail.getIsDefault().toString()); 127 edit.setSortOrder(protocolDetail.getSortOrder().toString()); 128 129 if(protocolDescription != null) { 130 edit.setDescription(protocolDescription.getDescription()); 131 } 132 } 133 134 @Override 135 public void canUpdate(Protocol protocol) { 136 var serverControl = Session.getModelController(ServerControl.class); 137 var protocolName = edit.getProtocolName(); 138 var duplicateProtocol = serverControl.getProtocolByName(protocolName); 139 140 if(duplicateProtocol != null && !protocol.equals(duplicateProtocol)) { 141 addExecutionError(ExecutionErrors.DuplicateProtocolName.name(), protocolName); 142 } 143 } 144 145 @Override 146 public void doUpdate(Protocol protocol) { 147 var serverControl = Session.getModelController(ServerControl.class); 148 var partyPK = getPartyPK(); 149 var protocolDetailValue = serverControl.getProtocolDetailValueForUpdate(protocol); 150 var protocolDescription = serverControl.getProtocolDescriptionForUpdate(protocol, getPreferredLanguage()); 151 var description = edit.getDescription(); 152 153 protocolDetailValue.setProtocolName(edit.getProtocolName()); 154 protocolDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 155 protocolDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 156 157 serverControl.updateProtocolFromValue(protocolDetailValue, partyPK); 158 159 if(protocolDescription == null && description != null) { 160 serverControl.createProtocolDescription(protocol, getPreferredLanguage(), description, partyPK); 161 } else { 162 if(protocolDescription != null && description == null) { 163 serverControl.deleteProtocolDescription(protocolDescription, partyPK); 164 } else { 165 if(protocolDescription != null && description != null) { 166 var protocolDescriptionValue = serverControl.getProtocolDescriptionValue(protocolDescription); 167 168 protocolDescriptionValue.setDescription(description); 169 serverControl.updateProtocolDescriptionFromValue(protocolDescriptionValue, partyPK); 170 } 171 } 172 } 173 } 174 175}