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.GetContentCategoryForm;
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.control.core.common.EventTypes;
024import com.echothree.model.data.content.server.entity.ContentCategory;
025import com.echothree.model.data.content.server.entity.ContentCollection;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseSingleEntityCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035
036@Dependent
037public class GetContentCategoryCommand
038        extends BaseSingleEntityCommand<ContentCategory, GetContentCategoryForm> {
039    
040    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null),
046                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("ContentCategoryName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null),
050                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
051                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
052                );
053    }
054    
055    /** Creates a new instance of GetContentCategoryCommand */
056    public GetContentCategoryCommand() {
057        super(null, FORM_FIELD_DEFINITIONS, true);
058    }
059    
060    @Override
061    protected ContentCategory getEntity() {
062        var contentWebAddressName = form.getContentWebAddressName();
063        var contentCollectionName = form.getContentCollectionName();
064        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
065        ContentCategory contentCategory = null;
066
067        if(parameterCount == 1) {
068            var contentControl = Session.getModelController(ContentControl.class);
069            ContentCollection contentCollection = null;
070
071            if(contentWebAddressName != null) {
072                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
073
074                if(contentWebAddress != null) {
075                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
076                } else {
077                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
078                }
079            } else {
080                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
081
082                if(contentCollection == null) {
083                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
084                }
085            }
086
087            if(!hasExecutionErrors()) {
088                var contentCatalogName = form.getContentCatalogName();
089                var contentCategoryName = form.getContentCategoryName();
090                var partyPK = getPartyPK();
091
092                var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection)
093                        : contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
094
095                if(contentCatalog != null) {
096                    contentCategory = contentCategoryName == null ? contentControl.getDefaultContentCategory(contentCatalog)
097                            : contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
098
099                    if(contentCategory != null) {
100                        AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(),
101                                contentCategory.getPrimaryKey(), partyPK);
102
103                        if(!hasExecutionErrors()) {
104                            sendEvent(contentCategory.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
105                        }
106                    } else {
107                        addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(),
108                                contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName, contentCategoryName);
109                    }
110                } else {
111                    addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(),
112                            contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
113                }
114            }
115        } else {
116            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
117        }
118
119        return contentCategory;
120    }
121    
122    @Override
123    protected BaseResult getResult(ContentCategory contentCategory) {
124        var result = ContentResultFactory.getGetContentCategoryResult();
125
126        if (contentCategory != null) {
127            var contentControl = Session.getModelController(ContentControl.class);
128
129            result.setContentCategory(contentControl.getContentCategoryTransfer(getUserVisit(), contentCategory));
130        }
131
132        return result;
133    }
134    
135}