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