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.form.GetContentCategoriesForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.model.control.associate.server.logic.AssociateReferralLogic;
022import com.echothree.model.control.content.server.control.ContentControl;
023import com.echothree.model.data.content.server.entity.ContentCatalog;
024import com.echothree.model.data.content.server.entity.ContentCategory;
025import com.echothree.model.data.content.server.entity.ContentCollection;
026import com.echothree.model.data.content.server.factory.ContentCategoryFactory;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
033import com.echothree.util.server.persistence.Session;
034import java.util.Arrays;
035import java.util.Collection;
036import java.util.Collections;
037import java.util.List;
038import javax.enterprise.context.RequestScoped;
039
040@RequestScoped
041public class GetContentCategoriesCommand
042        extends BasePaginatedMultipleEntitiesCommand<ContentCategory, GetContentCategoriesForm> {
043    
044    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
049                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null),
050                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null),
052                new FieldDefinition("ParentContentCategoryName", FieldType.ENTITY_NAME, false, null, null),
053                new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null),
054                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
055                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
056                ));
057    }
058    
059    /** Creates a new instance of GetContentCategoriesCommand */
060    public GetContentCategoriesCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, true);
062    }
063    
064    private ContentCatalog contentCatalog;
065    private ContentCategory parentContentCategory;
066
067    @Override
068    protected void handleForm() {
069        var contentWebAddressName = form.getContentWebAddressName();
070        var contentCollectionName = form.getContentCollectionName();
071        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
072
073        if(parameterCount == 1) {
074            var contentControl = Session.getModelController(ContentControl.class);
075            ContentCollection contentCollection = null;
076
077            if(contentWebAddressName != null) {
078                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
079
080                if(contentWebAddress != null) {
081                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
082                } else {
083                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
084                }
085            } else {
086                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
087
088                if(contentCollection == null) {
089                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
090                }
091            }
092
093            if(!hasExecutionErrors()) {
094                var contentCatalogName = form.getContentCatalogName();
095
096                contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
097
098                if(contentCatalogName == null || contentCatalog != null) {
099                    if(contentCatalog == null) {
100                        contentCatalog = contentControl.getDefaultContentCatalog(contentCollection);
101                    }
102
103                    if(contentCatalog != null) {
104                        var parentContentCategoryName = form.getParentContentCategoryName();
105
106                        if(parentContentCategoryName == null) {
107                            parentContentCategory = null;
108                        } else {
109                            parentContentCategory = contentControl.getContentCategoryByName(contentCatalog, parentContentCategoryName);
110
111                            if(parentContentCategory == null) {
112                                addExecutionError(ExecutionErrors.UnknownParentContentCategoryName.name(),
113                                        contentCollection.getLastDetail().getContentCollectionName(), parentContentCategoryName);
114                            }
115                        }
116
117                    } else {
118                        addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(),
119                                contentCollection.getLastDetail().getContentCollectionName());
120                    }
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(),
123                            contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
124                }
125            }
126        } else {
127            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
128        }
129
130        if(!hasExecutionErrors()) {
131            AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(),
132                    contentCatalog.getPrimaryKey(), getPartyPK());
133        }
134    }
135
136    @Override
137    protected Long getTotalEntities() {
138        var contentControl = Session.getModelController(ContentControl.class);
139
140        return hasExecutionErrors() ? null :
141                parentContentCategory == null ?
142                        contentControl.countContentCategoriesByContentCatalog(contentCatalog) :
143                        contentControl.countContentCategoriesByParentContentCategory(parentContentCategory);
144    }
145
146    
147    @Override
148    protected Collection<ContentCategory> getEntities() {
149        Collection<ContentCategory> contentCategories = null;
150
151        if(!hasExecutionErrors()) {
152            var contentControl = Session.getModelController(ContentControl.class);
153
154            if(parentContentCategory == null) {
155                contentCategories = contentControl.getContentCategories(contentCatalog);
156            } else {
157                contentCategories = contentControl.getContentCategoriesByParentContentCategory(parentContentCategory);
158            }
159        }
160
161        return contentCategories;
162    }
163    
164    @Override
165    protected BaseResult getResult(Collection<ContentCategory> entities) {
166        var result = ContentResultFactory.getGetContentCategoriesResult();
167
168        if(entities != null) {
169            var contentControl = Session.getModelController(ContentControl.class);
170            var userVisit = getUserVisit();
171
172            result.setContentCatalog(contentControl.getContentCatalogTransfer(userVisit, contentCatalog));
173
174            if(parentContentCategory != null) {
175                result.setParentContentCategory(contentControl.getContentCategoryTransfer(userVisit, parentContentCategory));
176            }
177
178            if(session.hasLimit(ContentCategoryFactory.class)) {
179                result.setContentCategoryCount(getTotalEntities());
180            }
181
182            result.setContentCategories(contentControl.getContentCategoryTransfers(userVisit, entities));
183        }
184
185        return result;
186    }
187    
188}