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.employee.server.command; 018 019import com.echothree.control.user.core.common.spec.UniversalEntitySpec; 020import com.echothree.control.user.employee.common.form.GetEmployeeForm; 021import com.echothree.control.user.employee.common.result.EmployeeResultFactory; 022import com.echothree.model.control.core.common.EventTypes; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.employee.server.control.EmployeeControl; 025import com.echothree.model.control.employee.server.logic.PartyEmployeeLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.logic.PartyLogic; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.employee.server.entity.PartyEmployee; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.command.SecurityResult; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseSingleEntityCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.List; 042 043public class GetEmployeeCommand 044 extends BaseSingleEntityCommand<PartyEmployee, GetEmployeeForm> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 051 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 052 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.Employee.name(), SecurityRoles.Review.name()) 055 )) 056 )); 057 058 FORM_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("EmployeeName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 062 new FieldDefinition("Key", FieldType.KEY, false, null, null), 063 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 064 new FieldDefinition("Ulid", FieldType.ULID, false, null, null) 065 ); 066 } 067 068 /** Creates a new instance of GetEmployeeCommand */ 069 public GetEmployeeCommand(UserVisitPK userVisitPK, GetEmployeeForm form) { 070 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 071 } 072 073 String employeeName; 074 String partyName; 075 UniversalEntitySpec universalEntitySpec; 076 int parameterCount; 077 078 @Override 079 public SecurityResult security() { 080 var securityResult = super.security(); 081 082 employeeName = form.getEmployeeName(); 083 partyName = form.getPartyName(); 084 universalEntitySpec = form; 085 parameterCount = (employeeName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 086 EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 087 088 if(!canSpecifyParty() && parameterCount != 0) { 089 securityResult = getInsufficientSecurityResult(); 090 } 091 092 return securityResult; 093 } 094 095 @Override 096 protected PartyEmployee getEntity() { 097 PartyEmployee partyEmployee = null; 098 099 if(parameterCount == 0) { 100 var party = getParty(); 101 102 PartyLogic.getInstance().checkPartyType(this, party, PartyTypes.EMPLOYEE.name()); 103 104 if(!hasExecutionErrors()) { 105 var employeeControl = Session.getModelController(EmployeeControl.class); 106 107 partyEmployee = employeeControl.getPartyEmployee(party); 108 } 109 } else { 110 partyEmployee = PartyEmployeeLogic.getInstance().getPartyEmployeeByName(this, employeeName, partyName, universalEntitySpec); 111 } 112 113 if(partyEmployee != null) { 114 sendEvent(partyEmployee.getPartyPK(), EventTypes.READ, null, null, getPartyPK()); 115 } 116 117 return partyEmployee; 118 } 119 120 @Override 121 protected BaseResult getResult(PartyEmployee partyEmployee) { 122 var result = EmployeeResultFactory.getGetEmployeeResult(); 123 124 if(partyEmployee != null) { 125 var employeeControl = Session.getModelController(EmployeeControl.class); 126 127 result.setEmployee(employeeControl.getEmployeeTransfer(getUserVisit(), partyEmployee)); 128 } 129 130 return result; 131 } 132 133}