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.GetContentCategoryItemsForm;
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.ContentCategory;
024import com.echothree.model.data.content.server.entity.ContentCategoryItem;
025import com.echothree.model.data.content.server.entity.ContentCollection;
026import com.echothree.model.data.content.server.factory.ContentCategoryItemFactory;
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 java.util.Collection;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class GetContentCategoryItemsCommand
040        extends BasePaginatedMultipleEntitiesCommand<ContentCategoryItem, GetContentCategoryItemsForm> {
041    
042    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null),
048                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null),
052                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
053                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
054        );
055    }
056
057    @Inject
058    ContentControl contentControl;
059
060    @Inject
061    AssociateReferralLogic associateReferralLogic;
062
063    
064    /** Creates a new instance of GetContentCategoryItemsCommand */
065    public GetContentCategoryItemsCommand() {
066        super(null, FORM_FIELD_DEFINITIONS, true);
067    }
068    
069    private ContentCategory contentCategory;
070
071    @Override
072    protected void handleForm() {
073        var contentWebAddressName = form.getContentWebAddressName();
074        var contentCollectionName = form.getContentCollectionName();
075        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
076
077        if(parameterCount == 1) {
078            ContentCollection contentCollection = null;
079
080            if(contentWebAddressName != null) {
081                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
082
083                if(contentWebAddress != null) {
084                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
085                } else {
086                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
087                }
088            } else {
089                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
090
091                if(contentCollection == null) {
092                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
093                }
094            }
095
096            if(!hasExecutionErrors()) {
097                var contentCatalogName = form.getContentCatalogName();
098                var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection)
099                        : contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
100
101                if(contentCatalog != null) {
102                    var contentCategoryName = form.getContentCategoryName();
103
104                    if(contentCategoryName == null) {
105                        contentCategory = contentControl.getDefaultContentCategory(contentCatalog);
106
107                        if(contentCategory == null) {
108                            addExecutionError(ExecutionErrors.UnknownDefaultContentCategory.name(),
109                                    contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
110                        }
111
112                    } else {
113                        contentCategory = contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
114
115                        if(contentCategory == null) {
116                            addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(),
117                                    contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName, contentCategoryName);
118                        }
119                    }
120                } else {
121                    if(contentCatalogName == null) {
122                        addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(),
123                                contentCollection.getLastDetail().getContentCollectionName());
124                    } else {
125                        addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(),
126                                contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
127                    }
128                }
129            }
130        } else {
131            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
132        }
133
134        if(!hasExecutionErrors()) {
135            associateReferralLogic.handleAssociateReferral(session, this, form, getUserVisit(),
136                    contentCategory.getPrimaryKey(), getPartyPK());
137        }
138    }
139
140    @Override
141    protected Long getTotalEntities() {
142        return hasExecutionErrors() ? null :
143                contentControl.countContentCategoryItemsByContentCategory(contentCategory);
144    }
145    
146    @Override
147    protected Collection<ContentCategoryItem> getEntities() {
148        Collection<ContentCategoryItem> contentCategoryItems = null;
149
150        if(!hasExecutionErrors()) {
151            contentCategoryItems = contentControl.getContentCategoryItemsByContentCategory(contentCategory);
152        }
153
154        return contentCategoryItems;
155    }
156    
157    @Override
158    protected BaseResult getResult(Collection<ContentCategoryItem> entities) {
159        var result = ContentResultFactory.getGetContentCategoryItemsResult();
160
161        if(entities != null) {
162            var userVisit = getUserVisit();
163
164            if(session.hasLimit(ContentCategoryItemFactory.class)) {
165                result.setContentCategoryItemCount(getTotalEntities());
166            }
167
168            result.setContentCategory(contentControl.getContentCategoryTransfer(userVisit, contentCategory));
169            result.setContentCategoryItems(contentControl.getContentCategoryItemTransfers(userVisit, entities));
170        }
171
172        return result;
173    }
174        
175}