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.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.Arrays;
034import java.util.Collections;
035import java.util.List;
036import javax.enterprise.context.RequestScoped;
037
038@RequestScoped
039public class GetContentCategoryCommand
040        extends BaseSingleEntityCommand<ContentCategory, GetContentCategoryForm> {
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 = Collections.unmodifiableList(Arrays.asList(
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    /** Creates a new instance of GetContentCategoryCommand */
058    public GetContentCategoryCommand() {
059        super(null, FORM_FIELD_DEFINITIONS, true);
060    }
061    
062    @Override
063    protected ContentCategory getEntity() {
064        var contentWebAddressName = form.getContentWebAddressName();
065        var contentCollectionName = form.getContentCollectionName();
066        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
067        ContentCategory contentCategory = null;
068
069        if(parameterCount == 1) {
070            var contentControl = Session.getModelController(ContentControl.class);
071            ContentCollection contentCollection = null;
072
073            if(contentWebAddressName != null) {
074                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
075
076                if(contentWebAddress != null) {
077                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
078                } else {
079                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
080                }
081            } else {
082                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
083
084                if(contentCollection == null) {
085                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
086                }
087            }
088
089            if(!hasExecutionErrors()) {
090                var contentCatalogName = form.getContentCatalogName();
091                var contentCategoryName = form.getContentCategoryName();
092                var partyPK = getPartyPK();
093
094                var contentCatalog = contentCatalogName == null ? contentControl.getDefaultContentCatalog(contentCollection)
095                        : contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
096
097                if(contentCatalog != null) {
098                    contentCategory = contentCategoryName == null ? contentControl.getDefaultContentCategory(contentCatalog)
099                            : contentControl.getContentCategoryByName(contentCatalog, contentCategoryName);
100
101                    if(contentCategory != null) {
102                        AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(),
103                                contentCategory.getPrimaryKey(), partyPK);
104
105                        if(!hasExecutionErrors()) {
106                            sendEvent(contentCategory.getPrimaryKey(), EventTypes.READ, null, null, partyPK);
107                        }
108                    } else {
109                        addExecutionError(ExecutionErrors.UnknownContentCategoryName.name(),
110                                contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName, contentCategoryName);
111                    }
112                } else {
113                    addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(),
114                            contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
115                }
116            }
117        } else {
118            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
119        }
120
121        return contentCategory;
122    }
123    
124    @Override
125    protected BaseResult getResult(ContentCategory contentCategory) {
126        var result = ContentResultFactory.getGetContentCategoryResult();
127
128        if (contentCategory != null) {
129            var contentControl = Session.getModelController(ContentControl.class);
130
131            result.setContentCategory(contentControl.getContentCategoryTransfer(getUserVisit(), contentCategory));
132        }
133
134        return result;
135    }
136    
137}