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.contactlist.server.command; 018 019import com.echothree.control.user.contactlist.common.form.GetPartyContactListsForm; 020import com.echothree.control.user.contactlist.common.result.ContactListResultFactory; 021import com.echothree.model.control.contactlist.server.control.ContactListControl; 022import com.echothree.model.control.contactlist.server.logic.ContactListLogic; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.party.server.control.PartyControl; 025import com.echothree.model.control.party.server.logic.PartyLogic; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.data.contactlist.server.entity.ContactList; 029import com.echothree.model.data.contactlist.server.entity.PartyContactList; 030import com.echothree.model.data.contactlist.server.factory.PartyContactListFactory; 031import com.echothree.model.data.party.server.entity.Party; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.Collection; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class GetPartyContactListsCommand 047 extends BasePaginatedMultipleEntitiesCommand<PartyContactList, GetPartyContactListsForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.PartyContactList.name(), SecurityRoles.List.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, false, null, null) 063 ); 064 } 065 066 @Inject 067 ContactListControl contactListControl; 068 069 @Inject 070 PartyControl partyControl; 071 072 @Inject 073 ContactListLogic contactListLogic; 074 075 @Inject 076 PartyLogic partyLogic; 077 078 079 /** Creates a new instance of GetPartyContactListsCommand */ 080 public GetPartyContactListsCommand() { 081 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 082 } 083 084 Party party; 085 ContactList contactList; 086 087 @Override 088 protected void handleForm() { 089 var partyName = form.getPartyName(); 090 var contactListName = form.getContactListName(); 091 var parameterCount = (partyName != null ? 1 : 0) + (contactListName != null ? 1 : 0); 092 093 if(parameterCount == 1) { 094 if(partyName != null) { 095 party = partyLogic.getPartyByName(this, partyName); 096 } else { 097 contactList = contactListLogic.getContactListByName(this, contactListName); 098 } 099 } else { 100 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 101 } 102 103 104 } 105 106 @Override 107 protected Long getTotalEntities() { 108 Long total = null; 109 110 if(!hasExecutionErrors()) { 111 if(party != null) { 112 total = contactListControl.countPartyContactListsByParty(party); 113 } else { 114 total = contactListControl.countPartyContactListsByContactList(contactList); 115 } 116 } 117 118 return total; 119 } 120 121 @Override 122 protected Collection<PartyContactList> getEntities() { 123 Collection<PartyContactList> partyContactLists = null; 124 125 if(!hasExecutionErrors()) { 126 if(party != null) { 127 partyContactLists = contactListControl.getPartyContactListsByParty(party); 128 } else { 129 partyContactLists = contactListControl.getPartyContactListsByContactList(contactList); 130 } 131 } 132 133 return partyContactLists; 134 } 135 136 @Override 137 protected BaseResult getResult(Collection<PartyContactList> entities) { 138 var result = ContactListResultFactory.getGetPartyContactListsResult(); 139 140 if(entities != null) { 141 var userVisit = getUserVisit(); 142 143 if(party != null) { 144 result.setParty(partyControl.getPartyTransfer(userVisit, party)); 145 } else { 146 result.setContactList(contactListControl.getContactListTransfer(userVisit, contactList)); 147 } 148 149 if(session.hasLimit(PartyContactListFactory.class)) { 150 result.setPartyContactListCount(getTotalEntities()); 151 } 152 153 result.setPartyContactLists(contactListControl.getPartyContactListTransfers(userVisit, entities)); 154 } 155 156 return result; 157 } 158 159}