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.edit.SearchEditFactory;
020import com.echothree.control.user.search.common.edit.SearchSortOrderEdit;
021import com.echothree.control.user.search.common.form.EditSearchSortOrderForm;
022import com.echothree.control.user.search.common.result.EditSearchSortOrderResult;
023import com.echothree.control.user.search.common.result.SearchResultFactory;
024import com.echothree.control.user.search.common.spec.SearchSortOrderSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.search.server.control.SearchControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.search.server.entity.SearchKind;
030import com.echothree.model.data.search.server.entity.SearchSortOrder;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditSearchSortOrderCommand
046        extends BaseAbstractEditCommand<SearchSortOrderSpec, SearchSortOrderEdit, EditSearchSortOrderResult, SearchSortOrder, SearchSortOrder> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_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.SearchSortOrder.name(), SecurityRoles.Edit.name())
057                ))
058        ));
059
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("SearchSortOrderName", FieldType.ENTITY_NAME, true, null, null)
062        );
063
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("SearchSortOrderName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
067                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
068                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
069        );
070    }
071
072    @Inject
073    SearchControl searchControl;
074
075    /** Creates a new instance of EditSearchSortOrderCommand */
076    public EditSearchSortOrderCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079
080    @Override
081    public EditSearchSortOrderResult getResult() {
082        return SearchResultFactory.getEditSearchSortOrderResult();
083    }
084
085    @Override
086    public SearchSortOrderEdit getEdit() {
087        return SearchEditFactory.getSearchSortOrderEdit();
088    }
089
090    SearchKind searchKind;
091
092    @Override
093    public SearchSortOrder getEntity(EditSearchSortOrderResult result) {
094        SearchSortOrder searchSortOrder = null;
095        var searchKindName = spec.getSearchKindName();
096
097        searchKind = searchControl.getSearchKindByName(searchKindName);
098
099        if(searchKind != null) {
100            var searchSortOrderName = spec.getSearchSortOrderName();
101
102            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
103                searchSortOrder = searchControl.getSearchSortOrderByName(searchKind, searchSortOrderName);
104            } else { // EditMode.UPDATE
105                searchSortOrder = searchControl.getSearchSortOrderByNameForUpdate(searchKind, searchSortOrderName);
106            }
107
108            if(searchSortOrder == null) {
109                addExecutionError(ExecutionErrors.UnknownSearchSortOrderName.name(), searchKindName, searchSortOrderName);
110            }
111        } else {
112            addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), searchKindName);
113        }
114
115        return searchSortOrder;
116    }
117
118    @Override
119    public SearchSortOrder getLockEntity(SearchSortOrder searchSortOrder) {
120        return searchSortOrder;
121    }
122
123    @Override
124    public void fillInResult(EditSearchSortOrderResult result, SearchSortOrder searchSortOrder) {
125        result.setSearchSortOrder(searchControl.getSearchSortOrderTransfer(getUserVisit(), searchSortOrder));
126    }
127
128    @Override
129    public void doLock(SearchSortOrderEdit edit, SearchSortOrder searchSortOrder) {
130        var searchSortOrderDescription = searchControl.getSearchSortOrderDescription(searchSortOrder, getPreferredLanguage());
131        var searchSortOrderDetail = searchSortOrder.getLastDetail();
132
133        edit.setSearchSortOrderName(searchSortOrderDetail.getSearchSortOrderName());
134        edit.setIsDefault(searchSortOrderDetail.getIsDefault().toString());
135        edit.setSortOrder(searchSortOrderDetail.getSortOrder().toString());
136
137        if(searchSortOrderDescription != null) {
138            edit.setDescription(searchSortOrderDescription.getDescription());
139        }
140    }
141
142    @Override
143    public void canUpdate(SearchSortOrder searchSortOrder) {
144        var searchKindDetail = searchKind.getLastDetail();
145        var searchSortOrderName = edit.getSearchSortOrderName();
146        var duplicateSearchSortOrder = searchControl.getSearchSortOrderByName(searchKind, searchSortOrderName);
147
148        if(duplicateSearchSortOrder != null && !searchSortOrder.equals(duplicateSearchSortOrder)) {
149            addExecutionError(ExecutionErrors.DuplicateSearchSortOrderName.name(), searchKindDetail.getSearchKindName(), searchSortOrderName);
150        }
151    }
152
153    @Override
154    public void doUpdate(SearchSortOrder searchSortOrder) {
155        var partyPK = getPartyPK();
156        var searchSortOrderDetailValue = searchControl.getSearchSortOrderDetailValueForUpdate(searchSortOrder);
157        var searchSortOrderDescription = searchControl.getSearchSortOrderDescriptionForUpdate(searchSortOrder, getPreferredLanguage());
158        var description = edit.getDescription();
159
160        searchSortOrderDetailValue.setSearchSortOrderName(edit.getSearchSortOrderName());
161        searchSortOrderDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
162        searchSortOrderDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
163
164        searchControl.updateSearchSortOrderFromValue(searchSortOrderDetailValue, partyPK);
165
166        if(searchSortOrderDescription == null && description != null) {
167            searchControl.createSearchSortOrderDescription(searchSortOrder, getPreferredLanguage(), description, partyPK);
168        } else if(searchSortOrderDescription != null && description == null) {
169            searchControl.deleteSearchSortOrderDescription(searchSortOrderDescription, partyPK);
170        } else if(searchSortOrderDescription != null && description != null) {
171            var searchSortOrderDescriptionValue = searchControl.getSearchSortOrderDescriptionValue(searchSortOrderDescription);
172
173            searchSortOrderDescriptionValue.setDescription(description);
174            searchControl.updateSearchSortOrderDescriptionFromValue(searchSortOrderDescriptionValue, partyPK);
175        }
176    }
177
178}