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 java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
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    @Inject
056    ContentControl contentControl;
057
058    @Inject
059    AssociateReferralLogic associateReferralLogic;
060
061    
062    /** Creates a new instance of GetContentCategoryCommand */
063    public GetContentCategoryCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, true);
065    }
066    
067    @Override
068    protected ContentCategory getEntity() {
069        var contentWebAddressName = form.getContentWebAddressName();
070        var contentCollectionName = form.getContentCollectionName();
071        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
072        ContentCategory contentCategory = null;
073
074        if(parameterCount == 1) {
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                var contentCategoryName = form.getContentCategoryName();
096                var partyPK = getPartyPK();
097
098                var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection)
099                        : contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
100
101                if(contentCatalog != null) {
102                    contentCategory = contentCategoryName == null ? contentControl.getDefaultContentCategory(contentCatalog)
103                            : contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
104
105                    if(contentCategory != null) {
106                        associateReferralLogic.handleAssociateReferral(session, this, form, getUserVisitForUpdate(),
107                                contentCategory.getPrimaryKey(), partyPK);
108
109                        if(!hasExecutionErrors()) {
110                            sendEvent(contentCategory.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
111                        }
112                    } else {
113                        addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(),
114                                contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName, contentCategoryName);
115                    }
116                } else {
117                    addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(),
118                            contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
119                }
120            }
121        } else {
122            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
123        }
124
125        return contentCategory;
126    }
127    
128    @Override
129    protected BaseResult getResult(ContentCategory contentCategory) {
130        var result = ContentResultFactory.getGetContentCategoryResult();
131
132        if (contentCategory != null) {
133            result.setContentCategory(contentControl.getContentCategoryTransfer(getUserVisit(), contentCategory));
134        }
135
136        return result;
137    }
138    
139}