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