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.search.server.command;
018
019import com.echothree.control.user.search.common.form.SearchWarehousesForm;
020import com.echothree.control.user.search.common.result.SearchResultFactory;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.search.common.SearchKinds;
024import com.echothree.model.control.search.server.control.SearchControl;
025import com.echothree.model.control.search.server.logic.SearchLogic;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.warehouse.server.search.WarehouseSearchEvaluator;
029import com.echothree.model.data.party.server.entity.PartyAliasType;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.BaseResult;
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.BaseSimpleCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.google.common.base.Splitter;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class SearchWarehousesCommand
046        extends BaseSimpleCommand<SearchWarehousesForm> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_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.Warehouse.name(), SecurityRoles.Search.name())
056                ))
057        ));
058        
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("Q", FieldType.STRING, false, null, null),
062                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null),
067                new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null),
068                new FieldDefinition("Fields", FieldType.STRING, false, null, null)
069        );
070    }
071
072    @Inject
073    PartyControl partyControl;
074
075    @Inject
076    SearchControl searchControl;
077
078    @Inject
079    SearchLogic searchLogic;
080
081    /** Creates a new instance of SearchWarehousesCommand */
082    public SearchWarehousesCommand() {
083        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
084    }
085    
086    @Override
087    protected BaseResult execute() {
088        var result = SearchResultFactory.getSearchWarehousesResult();
089        var searchKind = searchControl.getSearchKindByName(SearchKinds.WAREHOUSE.name());
090        
091        if(searchKind != null) {
092            var searchTypeName = form.getSearchTypeName();
093            var searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName);
094            
095            if(searchType != null) {
096                var partyAliasTypeName = form.getPartyAliasTypeName();
097                var alias = form.getAlias();
098                PartyAliasType partyAliasType = null;
099
100                if(partyAliasTypeName != null) {
101                    var partyType = partyControl.getPartyTypeByName(PartyTypes.WAREHOUSE.name());
102
103                    if(partyType != null) {
104                        partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName);
105
106                        if(partyAliasType == null) {
107                            addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), PartyTypes.WAREHOUSE.name(), partyAliasTypeName);
108                        }
109                    } else {
110                        addExecutionError(ExecutionErrors.UnknownPartyTypeName.name(), PartyTypes.WAREHOUSE.name());
111                    }
112                }
113
114                if(!hasExecutionErrors()) {
115                    var userVisit = getUserVisit();
116                    var warehouseSearchEvaluator = new WarehouseSearchEvaluator(userVisit, searchType,
117                            searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind),
118                            searchLogic.getDefaultSearchSortDirection(null));
119                    var createdSince = form.getCreatedSince();
120                    var modifiedSince = form.getModifiedSince();
121                    var fields = form.getFields();
122
123                    warehouseSearchEvaluator.setQ(this, form.getQ());
124                    warehouseSearchEvaluator.setPartyAliasType(partyAliasType);
125                    warehouseSearchEvaluator.setAlias(alias);
126                    warehouseSearchEvaluator.setWarehouseName(form.getWarehouseName());
127                    warehouseSearchEvaluator.setPartyName(form.getPartyName());
128                    warehouseSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
129                    warehouseSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
130                    warehouseSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
131
132                    if(!hasExecutionErrors()) {
133                        result.setCount(warehouseSearchEvaluator.execute(this));
134                    }
135                }
136            } else {
137                addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.WAREHOUSE.name(), searchTypeName);
138            }
139        } else {
140            addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.WAREHOUSE.name());
141        }
142        
143        return result;
144    }
145}