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.content.server.command;
018
019import com.echothree.control.user.content.common.form.CreateContentCategoryForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.model.control.content.common.ContentCategories;
022import com.echothree.model.control.content.server.control.ContentControl;
023import com.echothree.model.control.offer.server.control.OfferControl;
024import com.echothree.model.control.offer.server.control.OfferUseControl;
025import com.echothree.model.control.offer.server.control.SourceControl;
026import com.echothree.model.control.offer.server.control.UseControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.control.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.control.selector.common.SelectorKinds;
032import com.echothree.model.control.selector.common.SelectorTypes;
033import com.echothree.model.control.selector.server.control.SelectorControl;
034import com.echothree.model.data.content.server.entity.ContentCategory;
035import com.echothree.model.data.offer.server.entity.OfferUse;
036import com.echothree.model.data.selector.server.entity.Selector;
037import com.echothree.model.data.user.common.pk.UserVisitPK;
038import com.echothree.util.common.command.BaseResult;
039import com.echothree.util.common.message.ExecutionErrors;
040import com.echothree.util.common.validation.FieldDefinition;
041import com.echothree.util.common.validation.FieldType;
042import com.echothree.util.server.control.BaseSimpleCommand;
043import com.echothree.util.server.control.CommandSecurityDefinition;
044import com.echothree.util.server.control.PartyTypeDefinition;
045import com.echothree.util.server.control.SecurityRoleDefinition;
046import com.echothree.util.server.persistence.Session;
047import java.util.List;
048import javax.enterprise.context.Dependent;
049
050@Dependent
051public class CreateContentCategoryCommand
052        extends BaseSimpleCommand<CreateContentCategoryForm> {
053    
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
056    
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
061                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCategory.name(), SecurityRoles.Create.name())
062                        ))
063                ));
064        
065        FORM_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("ParentContentCategoryName", FieldType.ENTITY_NAME, false, null, null),
070                new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null),
071                new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null),
072                new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null),
073                new FieldDefinition("ContentCategoryItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
074                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
075                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
076                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
077                );
078    }
079    
080    /** Creates a new instance of CreateContentCategoryCommand */
081    public CreateContentCategoryCommand() {
082        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
083    }
084    
085    @Override
086    protected BaseResult execute() {
087        var result = ContentResultFactory.getCreateContentCategoryResult();
088        var contentControl = Session.getModelController(ContentControl.class);
089        var contentCollectionName = form.getContentCollectionName();
090        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
091        ContentCategory contentCategory = null;
092        
093        if(contentCollection != null) {
094            var contentCatalogName = form.getContentCatalogName();
095            var contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
096            
097            if(contentCatalog != null) {
098                var contentCategoryName = form.getContentCategoryName();
099                
100                contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
101                
102                if(contentCategory == null) {
103                    var parentContentCategoryName = form.getParentContentCategoryName();
104                    
105                    if(parentContentCategoryName == null)
106                        parentContentCategoryName = ContentCategories.ROOT.toString();
107
108                    var parentContentCategory = parentContentCategoryName == null? null: contentControl.getContentCategoryByName(contentCatalog,
109                            parentContentCategoryName);
110                    
111                    if(parentContentCategory == null) {
112                        addExecutionError(ExecutionErrors.UnknownParentContentCategoryName.name(), parentContentCategoryName);
113                    } else {
114                        var offerControl = Session.getModelController(OfferControl.class);
115                        var defaultOfferName = form.getDefaultOfferName();
116                        var defaultUseName = form.getDefaultUseName();
117                        var defaultSourceName = form.getDefaultSourceName();
118                        OfferUse defaultOfferUse = null;
119                        var invalidDefaultOfferOrSourceSpecification = false;
120                        
121                        if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) {
122                            var defaultOffer = offerControl.getOfferByName(defaultOfferName);
123                            
124                            if(defaultOffer != null) {
125                                var useControl = Session.getModelController(UseControl.class);
126                                var defaultUse = useControl.getUseByName(defaultUseName);
127                                
128                                if(defaultUse != null) {
129                                    var offerUseControl = Session.getModelController(OfferUseControl.class);
130                                    defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse);
131                                    
132                                    if(defaultOfferUse == null) {
133                                        addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name());
134                                    }
135                                }  else {
136                                    addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName);
137                                }
138                            } else {
139                                addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName);
140                            }
141                        } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) {
142                            var sourceControl = Session.getModelController(SourceControl.class);
143                            var source = sourceControl.getSourceByName(defaultSourceName);
144                            
145                            if(source != null) {
146                                defaultOfferUse = source.getLastDetail().getOfferUse();
147                            } else {
148                                addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName);
149                            }
150                        } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName == null) {
151                            // nothing
152                        } else {
153                            addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name());
154                            invalidDefaultOfferOrSourceSpecification = true;
155                        }
156                        
157                        if(!invalidDefaultOfferOrSourceSpecification) {
158                            var invalidOfferCompany = false;
159                            
160                            if(defaultOfferUse != null) {
161                                var partyControl = Session.getModelController(PartyControl.class);
162                                var defaultOffer = defaultOfferUse.getLastDetail().getOffer();
163                                var defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty());
164                                var defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty());
165                                var defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty());
166
167                                var catalogOffer = contentCatalog.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer();
168                                var catalogPartyDepartment = partyControl.getPartyDepartment(catalogOffer.getLastDetail().getDepartmentParty());
169                                var catalogPartyDivision = partyControl.getPartyDivision(catalogPartyDepartment.getDivisionParty());
170                                var catalogPartyCompany = partyControl.getPartyCompany(catalogPartyDivision.getCompanyParty());
171                                
172                                if(!defaultPartyCompany.equals(catalogPartyCompany)) {
173                                    invalidOfferCompany = true;
174                                }
175                            }
176                            
177                            if(!invalidOfferCompany) {
178                                var contentCategoryItemSelectorName = form.getContentCategoryItemSelectorName();
179                                Selector contentCategoryItemSelector = null;
180                                
181                                if(contentCategoryItemSelectorName != null) {
182                                    var selectorControl = Session.getModelController(SelectorControl.class);
183                                    var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
184                                    
185                                    if(selectorKind != null) {
186                                        var selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CONTENT_CATEGORY.name());
187                                        
188                                        if(selectorType != null) {
189                                            contentCategoryItemSelector = selectorControl.getSelectorByName(selectorType, contentCategoryItemSelectorName);
190                                        } else {
191                                            addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.CONTENT_CATEGORY.name());
192                                        }
193                                    } else {
194                                        addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
195                                    }
196                                }
197                                
198                                if(contentCategoryItemSelectorName != null && contentCategoryItemSelector == null) {
199                                    addExecutionError(ExecutionErrors.UnknownContentCategoryItemSelectorName.name(), contentCategoryItemSelectorName);
200                                } else {
201                                    var isDefault = Boolean.valueOf(form.getIsDefault());
202                                    var sortOrder = Integer.valueOf(form.getSortOrder());
203                                    var description = form.getDescription();
204                                    var createdBy = getPartyPK();
205                                    
206                                    contentCategory = contentControl.createContentCategory(contentCatalog, contentCategoryName,
207                                            parentContentCategory, defaultOfferUse, contentCategoryItemSelector, isDefault,
208                                            sortOrder, createdBy);
209                                    
210                                    if(description != null) {
211                                        contentControl.createContentCategoryDescription(contentCategory, getPreferredLanguage(),
212                                                description, createdBy);
213                                    }
214                                }
215                            } else {
216                                addExecutionError(ExecutionErrors.InvalidOfferCompany.name());
217                            }
218                        }
219                    }
220                } else {
221                    addExecutionError(ExecutionErrors.DuplicateContentCategoryName.name(), contentCategoryName);
222                }
223            } else {
224                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
225            }
226        } else {
227            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
228        }
229
230        if(contentCategory != null) {
231            var contentCategoryDetail = contentCategory.getLastDetail();
232            var contentCatalogDetail = contentCategoryDetail.getContentCatalog().getLastDetail();
233
234            result.setContentCollectionName(contentCatalogDetail.getContentCollection().getLastDetail().getContentCollectionName());
235            result.setContentCatalogName(contentCatalogDetail.getContentCatalogName());
236            result.setContentCategoryName(contentCategoryDetail.getContentCategoryName());
237            result.setEntityRef(contentCategory.getPrimaryKey().getEntityRef());
238        }
239
240        return result;
241    }
242    
243}