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 com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
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    /** Creates a new instance of EditSearchSortOrderCommand */
073    public EditSearchSortOrderCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076
077    @Override
078    public EditSearchSortOrderResult getResult() {
079        return SearchResultFactory.getEditSearchSortOrderResult();
080    }
081
082    @Override
083    public SearchSortOrderEdit getEdit() {
084        return SearchEditFactory.getSearchSortOrderEdit();
085    }
086
087    SearchKind searchKind;
088
089    @Override
090    public SearchSortOrder getEntity(EditSearchSortOrderResult result) {
091        var searchControl = Session.getModelController(SearchControl.class);
092        SearchSortOrder searchSortOrder = null;
093        var searchKindName = spec.getSearchKindName();
094
095        searchKind = searchControl.getSearchKindByName(searchKindName);
096
097        if(searchKind != null) {
098            var searchSortOrderName = spec.getSearchSortOrderName();
099
100            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
101                searchSortOrder = searchControl.getSearchSortOrderByName(searchKind, searchSortOrderName);
102            } else { // EditMode.UPDATE
103                searchSortOrder = searchControl.getSearchSortOrderByNameForUpdate(searchKind, searchSortOrderName);
104            }
105
106            if(searchSortOrder == null) {
107                addExecutionError(ExecutionErrors.UnknownSearchSortOrderName.name(), searchKindName, searchSortOrderName);
108            }
109        } else {
110            addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), searchKindName);
111        }
112
113        return searchSortOrder;
114    }
115
116    @Override
117    public SearchSortOrder getLockEntity(SearchSortOrder searchSortOrder) {
118        return searchSortOrder;
119    }
120
121    @Override
122    public void fillInResult(EditSearchSortOrderResult result, SearchSortOrder searchSortOrder) {
123        var searchControl = Session.getModelController(SearchControl.class);
124
125        result.setSearchSortOrder(searchControl.getSearchSortOrderTransfer(getUserVisit(), searchSortOrder));
126    }
127
128    @Override
129    public void doLock(SearchSortOrderEdit edit, SearchSortOrder searchSortOrder) {
130        var searchControl = Session.getModelController(SearchControl.class);
131        var searchSortOrderDescription = searchControl.getSearchSortOrderDescription(searchSortOrder, getPreferredLanguage());
132        var searchSortOrderDetail = searchSortOrder.getLastDetail();
133
134        edit.setSearchSortOrderName(searchSortOrderDetail.getSearchSortOrderName());
135        edit.setIsDefault(searchSortOrderDetail.getIsDefault().toString());
136        edit.setSortOrder(searchSortOrderDetail.getSortOrder().toString());
137
138        if(searchSortOrderDescription != null) {
139            edit.setDescription(searchSortOrderDescription.getDescription());
140        }
141    }
142
143    @Override
144    public void canUpdate(SearchSortOrder searchSortOrder) {
145        var searchControl = Session.getModelController(SearchControl.class);
146        var searchKindDetail = searchKind.getLastDetail();
147        var searchSortOrderName = edit.getSearchSortOrderName();
148        var duplicateSearchSortOrder = searchControl.getSearchSortOrderByName(searchKind, searchSortOrderName);
149
150        if(duplicateSearchSortOrder != null && !searchSortOrder.equals(duplicateSearchSortOrder)) {
151            addExecutionError(ExecutionErrors.DuplicateSearchSortOrderName.name(), searchKindDetail.getSearchKindName(), searchSortOrderName);
152        }
153    }
154
155    @Override
156    public void doUpdate(SearchSortOrder searchSortOrder) {
157        var searchControl = Session.getModelController(SearchControl.class);
158        var partyPK = getPartyPK();
159        var searchSortOrderDetailValue = searchControl.getSearchSortOrderDetailValueForUpdate(searchSortOrder);
160        var searchSortOrderDescription = searchControl.getSearchSortOrderDescriptionForUpdate(searchSortOrder, getPreferredLanguage());
161        var description = edit.getDescription();
162
163        searchSortOrderDetailValue.setSearchSortOrderName(edit.getSearchSortOrderName());
164        searchSortOrderDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
165        searchSortOrderDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
166
167        searchControl.updateSearchSortOrderFromValue(searchSortOrderDetailValue, partyPK);
168
169        if(searchSortOrderDescription == null && description != null) {
170            searchControl.createSearchSortOrderDescription(searchSortOrder, getPreferredLanguage(), description, partyPK);
171        } else if(searchSortOrderDescription != null && description == null) {
172            searchControl.deleteSearchSortOrderDescription(searchSortOrderDescription, partyPK);
173        } else if(searchSortOrderDescription != null && description != null) {
174            var searchSortOrderDescriptionValue = searchControl.getSearchSortOrderDescriptionValue(searchSortOrderDescription);
175
176            searchSortOrderDescriptionValue.setDescription(description);
177            searchControl.updateSearchSortOrderDescriptionFromValue(searchSortOrderDescriptionValue, partyPK);
178        }
179    }
180
181}