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.search.server.command;
018
019import com.echothree.control.user.search.common.form.SearchVendorsForm;
020import com.echothree.control.user.search.common.result.SearchResultFactory;
021import com.echothree.control.user.search.common.result.SearchVendorsResult;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.search.common.SearchKinds;
025import com.echothree.model.control.search.server.control.SearchControl;
026import com.echothree.model.control.vendor.server.search.VendorSearchEvaluator;
027import com.echothree.model.control.search.server.logic.SearchLogic;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.party.server.entity.PartyAliasType;
031import com.echothree.model.data.party.server.entity.PartyType;
032import com.echothree.model.data.search.server.entity.SearchKind;
033import com.echothree.model.data.search.server.entity.SearchType;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.model.data.user.server.entity.UserVisit;
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.BaseResult;
040import com.echothree.util.server.control.BaseSimpleCommand;
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 com.google.common.base.Splitter;
046import java.util.Arrays;
047import java.util.Collections;
048import java.util.List;
049
050public class SearchVendorsCommand
051        extends BaseSimpleCommand<SearchVendorsForm> {
052    
053    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
054    private final static List<FieldDefinition> FORM_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.Vendor.name(), SecurityRoles.Search.name())
061                        )))
062                )));
063        
064        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
065                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("FirstName", FieldType.STRING, false, 1L, 20L),
067                new FieldDefinition("FirstNameSoundex", FieldType.BOOLEAN, false, null, null),
068                new FieldDefinition("MiddleName", FieldType.STRING, false, 1L, 20L),
069                new FieldDefinition("MiddleNameSoundex", FieldType.BOOLEAN, false, null, null),
070                new FieldDefinition("LastName", FieldType.STRING, false, 1L, 20L),
071                new FieldDefinition("LastNameSoundex", FieldType.BOOLEAN, false, null, null),
072                new FieldDefinition("Name", FieldType.STRING, false, null, null),
073                new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null),
074                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
075                new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
076                new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null),
077                new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null),
078                new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null),
079                new FieldDefinition("Fields", FieldType.STRING, false, null, null)
080                ));
081    }
082
083    /** Creates a new instance of SearchVendorsCommand */
084    public SearchVendorsCommand(UserVisitPK userVisitPK, SearchVendorsForm form) {
085        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
086    }
087    
088    @Override
089    protected BaseResult execute() {
090        SearchVendorsResult result = SearchResultFactory.getSearchVendorsResult();
091        var searchControl = Session.getModelController(SearchControl.class);
092        SearchKind searchKind = searchControl.getSearchKindByName(SearchKinds.VENDOR.name());
093        
094        if(searchKind != null) {
095            String searchTypeName = form.getSearchTypeName();
096            SearchType searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName);
097            
098            if(searchType != null) {
099                String partyAliasTypeName = form.getPartyAliasTypeName();
100                String alias = form.getAlias();
101                PartyAliasType partyAliasType = null;
102
103                if(partyAliasTypeName != null) {
104                    var partyControl = Session.getModelController(PartyControl.class);
105                    PartyType partyType = partyControl.getPartyTypeByName(PartyTypes.VENDOR.name());
106
107                    if(partyType != null) {
108                        partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName);
109
110                        if(partyAliasType == null) {
111                            addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), PartyTypes.VENDOR.name(), partyAliasTypeName);
112                        }
113                    } else {
114                        addExecutionError(ExecutionErrors.UnknownPartyTypeName.name(), PartyTypes.VENDOR.name());
115                    }
116                }
117
118                if(!hasExecutionErrors()) {
119                    SearchLogic searchLogic = SearchLogic.getInstance();
120                    UserVisit userVisit = getUserVisit();
121                    VendorSearchEvaluator vendorSearchEvaluator = new VendorSearchEvaluator(userVisit, searchType,
122                            searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind),
123                            searchLogic.getDefaultSearchSortDirection(null));
124                    String createdSince = form.getCreatedSince();
125                    String modifiedSince = form.getModifiedSince();
126                    String fields = form.getFields();
127
128                    vendorSearchEvaluator.setFirstName(form.getFirstName());
129                    vendorSearchEvaluator.setFirstNameSoundex(Boolean.parseBoolean(form.getFirstNameSoundex()));
130                    vendorSearchEvaluator.setMiddleName(form.getMiddleName());
131                    vendorSearchEvaluator.setMiddleNameSoundex(Boolean.parseBoolean(form.getMiddleNameSoundex()));
132                    vendorSearchEvaluator.setLastName(form.getLastName());
133                    vendorSearchEvaluator.setLastNameSoundex(Boolean.parseBoolean(form.getLastNameSoundex()));
134                    vendorSearchEvaluator.setQ(this, form.getName());
135                    vendorSearchEvaluator.setPartyAliasType(partyAliasType);
136                    vendorSearchEvaluator.setAlias(alias);
137                    vendorSearchEvaluator.setVendorName(form.getVendorName());
138                    vendorSearchEvaluator.setPartyName(form.getPartyName());
139                    vendorSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
140                    vendorSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
141                    vendorSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
142
143                    if(!hasExecutionErrors()) {
144                        result.setCount(vendorSearchEvaluator.execute(this));
145                    }
146                }
147            } else {
148                addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.VENDOR.name(), searchTypeName);
149            }
150        } else {
151            addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.VENDOR.name());
152        }
153        
154        return result;
155    }
156}