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.SearchKindEdit;
021import com.echothree.control.user.search.common.form.EditSearchKindForm;
022import com.echothree.control.user.search.common.result.EditSearchKindResult;
023import com.echothree.control.user.search.common.result.SearchResultFactory;
024import com.echothree.control.user.search.common.spec.SearchKindSpec;
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.SearchKindDescription;
031import com.echothree.model.data.search.server.entity.SearchKindDetail;
032import com.echothree.model.data.search.server.value.SearchKindDescriptionValue;
033import com.echothree.model.data.search.server.value.SearchKindDetailValue;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.server.control.BaseAbstractEditCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047
048public class EditSearchKindCommand
049        extends BaseAbstractEditCommand<SearchKindSpec, SearchKindEdit, EditSearchKindResult, SearchKind, SearchKind> {
050
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
053    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
054
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
059                        new SecurityRoleDefinition(SecurityRoleGroups.SearchKind.name(), SecurityRoles.Edit.name())
060                        )))
061                )));
062
063        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
064                new FieldDefinition("SearchKindName", FieldType.ENTITY_NAME, true, null, null)
065                ));
066
067        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("SearchKindName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
070                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
071                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
072                ));
073    }
074
075    /** Creates a new instance of EditSearchKindCommand */
076    public EditSearchKindCommand(UserVisitPK userVisitPK, EditSearchKindForm form) {
077        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079
080    @Override
081    public EditSearchKindResult getResult() {
082        return SearchResultFactory.getEditSearchKindResult();
083    }
084
085    @Override
086    public SearchKindEdit getEdit() {
087        return SearchEditFactory.getSearchKindEdit();
088    }
089
090    @Override
091    public SearchKind getEntity(EditSearchKindResult result) {
092        var searchControl = Session.getModelController(SearchControl.class);
093        SearchKind searchKind;
094        String searchKindName = spec.getSearchKindName();
095
096        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
097            searchKind = searchControl.getSearchKindByName(searchKindName);
098        } else { // EditMode.UPDATE
099            searchKind = searchControl.getSearchKindByNameForUpdate(searchKindName);
100        }
101
102        if(searchKind == null) {
103            addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), searchKindName);
104        }
105
106        return searchKind;
107    }
108
109    @Override
110    public SearchKind getLockEntity(SearchKind searchKind) {
111        return searchKind;
112    }
113
114    @Override
115    public void fillInResult(EditSearchKindResult result, SearchKind searchKind) {
116        var searchControl = Session.getModelController(SearchControl.class);
117
118        result.setSearchKind(searchControl.getSearchKindTransfer(getUserVisit(), searchKind));
119    }
120
121    @Override
122    public void doLock(SearchKindEdit edit, SearchKind searchKind) {
123        var searchControl = Session.getModelController(SearchControl.class);
124        SearchKindDescription searchKindDescription = searchControl.getSearchKindDescription(searchKind, getPreferredLanguage());
125        SearchKindDetail searchKindDetail = searchKind.getLastDetail();
126
127        edit.setSearchKindName(searchKindDetail.getSearchKindName());
128        edit.setIsDefault(searchKindDetail.getIsDefault().toString());
129        edit.setSortOrder(searchKindDetail.getSortOrder().toString());
130
131        if(searchKindDescription != null) {
132            edit.setDescription(searchKindDescription.getDescription());
133        }
134    }
135
136    @Override
137    public void canUpdate(SearchKind searchKind) {
138        var searchControl = Session.getModelController(SearchControl.class);
139        String searchKindName = edit.getSearchKindName();
140        SearchKind duplicateSearchKind = searchControl.getSearchKindByName(searchKindName);
141
142        if(duplicateSearchKind != null && !searchKind.equals(duplicateSearchKind)) {
143            addExecutionError(ExecutionErrors.DuplicateSearchKindName.name(), searchKindName);
144        }
145    }
146
147    @Override
148    public void doUpdate(SearchKind searchKind) {
149        var searchControl = Session.getModelController(SearchControl.class);
150        var partyPK = getPartyPK();
151        SearchKindDetailValue searchKindDetailValue = searchControl.getSearchKindDetailValueForUpdate(searchKind);
152        SearchKindDescription searchKindDescription = searchControl.getSearchKindDescriptionForUpdate(searchKind, getPreferredLanguage());
153        String description = edit.getDescription();
154
155        searchKindDetailValue.setSearchKindName(edit.getSearchKindName());
156        searchKindDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
157        searchKindDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
158
159        searchControl.updateSearchKindFromValue(searchKindDetailValue, partyPK);
160
161        if(searchKindDescription == null && description != null) {
162            searchControl.createSearchKindDescription(searchKind, getPreferredLanguage(), description, partyPK);
163        } else if(searchKindDescription != null && description == null) {
164            searchControl.deleteSearchKindDescription(searchKindDescription, partyPK);
165        } else if(searchKindDescription != null && description != null) {
166            SearchKindDescriptionValue searchKindDescriptionValue = searchControl.getSearchKindDescriptionValue(searchKindDescription);
167
168            searchKindDescriptionValue.setDescription(description);
169            searchControl.updateSearchKindDescriptionFromValue(searchKindDescriptionValue, partyPK);
170        }
171    }
172
173}