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