001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.content.server.command;
018
019import com.echothree.control.user.content.common.edit.ContentCategoryEdit;
020import com.echothree.control.user.content.common.edit.ContentEditFactory;
021import com.echothree.control.user.content.common.form.EditContentCategoryForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentCategoryResult;
024import com.echothree.control.user.content.common.spec.ContentCategorySpec;
025import com.echothree.model.control.content.common.ContentCategories;
026import com.echothree.model.control.content.server.control.ContentControl;
027import com.echothree.model.control.offer.server.control.OfferControl;
028import com.echothree.model.control.offer.server.control.OfferUseControl;
029import com.echothree.model.control.offer.server.control.SourceControl;
030import com.echothree.model.control.offer.server.control.UseControl;
031import com.echothree.model.control.party.common.PartyTypes;
032import com.echothree.model.control.party.server.control.PartyControl;
033import com.echothree.model.control.security.common.SecurityRoleGroups;
034import com.echothree.model.control.security.common.SecurityRoles;
035import com.echothree.model.control.selector.common.SelectorKinds;
036import com.echothree.model.control.selector.common.SelectorTypes;
037import com.echothree.model.control.selector.server.control.SelectorControl;
038import com.echothree.model.data.content.server.entity.ContentCatalog;
039import com.echothree.model.data.content.server.entity.ContentCategory;
040import com.echothree.model.data.offer.server.entity.OfferUse;
041import com.echothree.model.data.selector.server.entity.Selector;
042import com.echothree.model.data.user.common.pk.UserVisitPK;
043import com.echothree.util.common.command.EditMode;
044import com.echothree.util.common.message.ExecutionErrors;
045import com.echothree.util.common.validation.FieldDefinition;
046import com.echothree.util.common.validation.FieldType;
047import com.echothree.util.server.control.BaseAbstractEditCommand;
048import com.echothree.util.server.control.CommandSecurityDefinition;
049import com.echothree.util.server.control.PartyTypeDefinition;
050import com.echothree.util.server.control.SecurityRoleDefinition;
051import com.echothree.util.server.persistence.Session;
052import java.util.Arrays;
053import java.util.Collections;
054import java.util.List;
055import javax.enterprise.context.RequestScoped;
056
057@RequestScoped
058public class EditContentCategoryCommand
059        extends BaseAbstractEditCommand<ContentCategorySpec, ContentCategoryEdit, EditContentCategoryResult, ContentCategory, ContentCategory> {
060    
061    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
062    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
063    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
064    
065    static {
066        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
067                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
068                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
069                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCategory.name(), SecurityRoles.Edit.name())
070                        )))
071                )));
072        
073        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
074                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
075                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
076                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null)
077                ));
078        
079        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
080                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null),
081                new FieldDefinition("ParentContentCategoryName", FieldType.ENTITY_NAME, false, null, null),
082                new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null),
083                new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null),
084                new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null),
085                new FieldDefinition("ContentCategoryItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
086                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
087                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
088                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
089                ));
090    }
091    
092    /** Creates a new instance of EditContentCategoryCommand */
093    public EditContentCategoryCommand() {
094        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
095    }
096    
097    @Override
098    public EditContentCategoryResult getResult() {
099        return ContentResultFactory.getEditContentCategoryResult();
100    }
101    
102    @Override
103    public ContentCategoryEdit getEdit() {
104        return ContentEditFactory.getContentCategoryEdit();
105    }
106    
107    ContentCatalog contentCatalog = null;
108    
109    @Override
110    public ContentCategory getEntity(EditContentCategoryResult result) {
111        var contentControl = Session.getModelController(ContentControl.class);
112        ContentCategory contentCategory = null;
113        var contentCollectionName = spec.getContentCollectionName();
114        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
115        
116        if(contentCollection != null) {
117            var contentCatalogName = spec.getContentCatalogName();
118            
119            contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
120
121            if(contentCatalog != null) {
122                var contentCategoryName = spec.getContentCategoryName();
123
124                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
125                    contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
126                } else { // EditMode.UPDATE
127                    contentCategory = contentControl.getContentCategoryByNameForUpdate(contentCatalog, contentCategoryName);
128                }
129
130                if(contentCategory != null) {
131                    result.setContentCategory(contentControl.getContentCategoryTransfer(getUserVisit(), contentCategory));
132                } else {
133                    addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(), contentCategoryName);
134                }
135            } else {
136                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
137            }
138        } else {
139            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
140        }
141
142        return contentCategory;
143    }
144    
145    @Override
146    public ContentCategory getLockEntity(ContentCategory contentCategory) {
147        return contentCategory;
148    }
149    
150    @Override
151    public void fillInResult(EditContentCategoryResult result, ContentCategory contentCategory) {
152        var contentControl = Session.getModelController(ContentControl.class);
153        
154        result.setContentCategory(contentControl.getContentCategoryTransfer(getUserVisit(), contentCategory));
155    }
156    
157    @Override
158    public void doLock(ContentCategoryEdit edit, ContentCategory contentCategory) {
159        var contentControl = Session.getModelController(ContentControl.class);
160        var sourceControl = Session.getModelController(SourceControl.class);
161        var contentCategoryDescription = contentControl.getContentCategoryDescription(contentCategory, getPreferredLanguage());
162        var contentCategoryDetail = contentCategory.getLastDetail();
163        var parentContentCategory = contentCategoryDetail.getParentContentCategory();
164        var defaultOfferUse = contentCategoryDetail.getDefaultOfferUse();
165        var defaultOfferUseDetail = defaultOfferUse == null ? null : defaultOfferUse.getLastDetail();
166        var sources = defaultOfferUse == null ? null : sourceControl.getSourcesByOfferUse(defaultOfferUse); // List of Sources if there are any for the OfferUse
167        var defaultSourceName = sources == null ? null : sources.iterator().next().getLastDetail().getSourceName(); // From the List, the first available Source's SourceName
168        var contentCategoryItemSelector = contentCategoryDetail.getContentCategoryItemSelector();
169
170        edit.setContentCategoryName(contentCategoryDetail.getContentCategoryName());
171        edit.setParentContentCategoryName(parentContentCategory == null ? null : parentContentCategory.getLastDetail().getContentCategoryName());
172        edit.setDefaultOfferName(defaultSourceName == null ? (defaultOfferUseDetail == null? null: defaultOfferUseDetail.getOffer().getLastDetail().getOfferName()) : null);
173        edit.setDefaultUseName(defaultSourceName == null ? (defaultOfferUseDetail == null ? null : defaultOfferUseDetail.getUse().getLastDetail().getUseName()) : null);
174        edit.setDefaultSourceName(defaultSourceName);
175        edit.setContentCategoryItemSelectorName(contentCategoryItemSelector == null? null: contentCategoryItemSelector.getLastDetail().getSelectorName());
176        edit.setIsDefault(contentCategoryDetail.getIsDefault().toString());
177        edit.setSortOrder(contentCategoryDetail.getSortOrder().toString());
178
179        if(contentCategoryDescription != null) {
180            edit.setDescription(contentCategoryDescription.getDescription());
181        }
182    }
183    
184    ContentCategory parentContentCategory = null;
185    OfferUse defaultOfferUse = null;
186    Selector contentCategoryItemSelector = null;
187   
188    @Override
189    public void canUpdate(ContentCategory contentCategory) {
190        var contentControl = Session.getModelController(ContentControl.class);
191        var contentCategoryName = edit.getContentCategoryName();
192        var duplicateContentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
193
194        if(duplicateContentCategory == null || contentCategory.equals(duplicateContentCategory)) {
195            var parentContentCategoryName = edit.getParentContentCategoryName();
196            
197            parentContentCategory = contentControl.getContentCategoryByName(contentCatalog, parentContentCategoryName == null ?
198                    ContentCategories.ROOT.toString() : parentContentCategoryName);
199
200            if(parentContentCategory != null) {
201                if(contentControl.isParentContentCategorySafe(contentCategory, parentContentCategory)) {
202                    var offerControl = Session.getModelController(OfferControl.class);
203                    var defaultOfferName = edit.getDefaultOfferName();
204                    var defaultUseName = edit.getDefaultUseName();
205                    var defaultSourceName = edit.getDefaultSourceName();
206                    var invalidDefaultOfferOrSourceSpecification = false;
207
208                    if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) {
209                        var defaultOffer = offerControl.getOfferByName(defaultOfferName);
210
211                        if(defaultOffer != null) {
212                            var useControl = Session.getModelController(UseControl.class);
213                            var defaultUse = useControl.getUseByName(defaultUseName);
214
215                            if(defaultUse != null) {
216                                var offerUseControl = Session.getModelController(OfferUseControl.class);
217                                defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse);
218
219                                if(defaultOfferUse == null) {
220                                    addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name());
221                                }
222                            } else {
223                                addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName);
224                            }
225                        } else {
226                            addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName);
227                        }
228                    } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) {
229                        var sourceControl = Session.getModelController(SourceControl.class);
230                        var source = sourceControl.getSourceByName(defaultSourceName);
231
232                        if(source != null) {
233                            defaultOfferUse = source.getLastDetail().getOfferUse();
234                        } else {
235                            addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName);
236                        }
237                    } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName == null) {
238                        // nothing
239                    } else {
240                        addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name());
241                        invalidDefaultOfferOrSourceSpecification = true;
242                    }
243
244                    if(!invalidDefaultOfferOrSourceSpecification) {
245                        var invalidOfferCompany = false;
246
247                        if(defaultOfferUse != null) {
248                            var partyControl = Session.getModelController(PartyControl.class);
249                            var defaultOffer = defaultOfferUse.getLastDetail().getOffer();
250                            var defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty());
251                            var defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty());
252                            var defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty());
253
254                            var catalogOffer = contentCatalog.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer();
255                            var catalogPartyDepartment = partyControl.getPartyDepartment(catalogOffer.getLastDetail().getDepartmentParty());
256                            var catalogPartyDivision = partyControl.getPartyDivision(catalogPartyDepartment.getDivisionParty());
257                            var catalogPartyCompany = partyControl.getPartyCompany(catalogPartyDivision.getCompanyParty());
258
259                            if(!defaultPartyCompany.equals(catalogPartyCompany)) {
260                                invalidOfferCompany = true;
261                            }
262                        }
263
264                        if(!invalidOfferCompany) {
265                            var contentCategoryItemSelectorName = edit.getContentCategoryItemSelectorName();
266
267                            if(contentCategoryItemSelectorName != null) {
268                                var selectorControl = Session.getModelController(SelectorControl.class);
269                                var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
270
271                                if(selectorKind != null) {
272                                    var selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CONTENT_CATEGORY.name());
273
274                                    if(selectorType != null) {
275                                        contentCategoryItemSelector = selectorControl.getSelectorByName(selectorType, contentCategoryItemSelectorName);
276                                    } else {
277                                        addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.CONTENT_CATEGORY.name());
278                                    }
279                                } else {
280                                    addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
281                                }
282                            }
283
284                            if(contentCategoryItemSelectorName != null && contentCategoryItemSelector == null) {
285                                addExecutionError(ExecutionErrors.UnknownContentCategoryItemSelectorName.name(), contentCategoryItemSelectorName);
286                            }
287                        } else {
288                            addExecutionError(ExecutionErrors.InvalidOfferCompany.name());
289                        }
290                    }
291                } else {
292                    addExecutionError(ExecutionErrors.InvalidParentContentCategory.name());
293                }
294            } else {
295                addExecutionError(ExecutionErrors.UnknownParentContentCategoryName.name(), parentContentCategoryName);
296            }
297        } else {
298            addExecutionError(ExecutionErrors.DuplicateContentCategoryName.name(), contentCategoryName);
299        }
300    }
301    
302    @Override
303    public void doUpdate(ContentCategory contentCategory) {
304        var contentControl = Session.getModelController(ContentControl.class);
305        var partyPK = getPartyPK();
306        var contentCategoryDetailValue = contentControl.getContentCategoryDetailValueForUpdate(contentCategory);
307        var contentCategoryDescription = contentControl.getContentCategoryDescriptionForUpdate(contentCategory, getPreferredLanguage());
308        var description = edit.getDescription();
309
310        contentCategoryDetailValue.setContentCategoryName(edit.getContentCategoryName());
311        contentCategoryDetailValue.setParentContentCategoryPK(parentContentCategory == null ? null : parentContentCategory.getPrimaryKey());
312        contentCategoryDetailValue.setDefaultOfferUsePK(defaultOfferUse == null? null: defaultOfferUse.getPrimaryKey());
313        contentCategoryDetailValue.setContentCategoryItemSelectorPK(contentCategoryItemSelector == null? null: contentCategoryItemSelector.getPrimaryKey());
314        contentCategoryDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
315        contentCategoryDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
316
317        contentControl.updateContentCategoryFromValue(contentCategoryDetailValue, partyPK);
318
319        if(contentCategoryDescription == null && description != null) {
320            contentControl.createContentCategoryDescription(contentCategory, getPreferredLanguage(), description, partyPK);
321        } else if(contentCategoryDescription != null && description == null) {
322            contentControl.deleteContentCategoryDescription(contentCategoryDescription, partyPK);
323        } else if(contentCategoryDescription != null && description != null) {
324            var contentCategoryDescriptionValue = contentControl.getContentCategoryDescriptionValue(contentCategoryDescription);
325
326            contentCategoryDescriptionValue.setDescription(description);
327            contentControl.updateContentCategoryDescriptionFromValue(contentCategoryDescriptionValue, partyPK);
328        }
329    }
330    
331}