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.GetContentCatalogsForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.control.user.content.common.result.GetContentCatalogsResult;
022import com.echothree.model.control.associate.server.logic.AssociateReferralLogic;
023import com.echothree.model.control.content.server.control.ContentControl;
024import com.echothree.model.data.content.server.entity.ContentCatalog;
025import com.echothree.model.data.content.server.entity.ContentCollection;
026import com.echothree.model.data.content.server.entity.ContentWebAddress;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.model.data.user.server.entity.UserVisit;
029import com.echothree.util.common.command.BaseResult;
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.server.control.BaseMultipleEntitiesCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collection;
037import java.util.Collections;
038import java.util.List;
039
040public class GetContentCatalogsCommand
041        extends BaseMultipleEntitiesCommand<ContentCatalog, GetContentCatalogsForm> {
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("AssociateProgramName", FieldType.STRING, false, null, null),
051                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
052                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
053                ));
054    }
055    
056    /** Creates a new instance of GetContentCatalogsCommand */
057    public GetContentCatalogsCommand(UserVisitPK userVisitPK, GetContentCatalogsForm form) {
058        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
059    }
060    
061    ContentCollection contentCollection;
062
063    @Override
064    protected Collection<ContentCatalog> getEntities() {
065        String contentWebAddressName = form.getContentWebAddressName();
066        String contentCollectionName = form.getContentCollectionName();
067        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
068        Collection<ContentCatalog> contentCatalogs = null;
069
070        if(parameterCount == 1) {
071            var contentControl = Session.getModelController(ContentControl.class);
072
073            if(contentWebAddressName != null) {
074                ContentWebAddress 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 partyPK = getPartyPK();
091
092                AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(), contentCollection.getPrimaryKey(), partyPK);
093
094                if(!hasExecutionErrors()) {
095                    contentCatalogs = contentControl.getContentCatalogs(contentCollection);
096                }
097            }
098        } else {
099            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
100        }
101        
102        return contentCatalogs;
103    }
104    
105    @Override
106    protected BaseResult getResult(Collection<ContentCatalog> entities) {
107        GetContentCatalogsResult result = ContentResultFactory.getGetContentCatalogsResult();
108        
109        if(entities != null) {
110            var contentControl = Session.getModelController(ContentControl.class);
111            UserVisit userVisit = getUserVisit();
112            
113            result.setContentCollection(contentControl.getContentCollectionTransfer(userVisit, contentCollection));
114            result.setContentCatalogs(contentControl.getContentCatalogTransfers(userVisit, entities));
115        }
116        
117        return result;
118    }
119
120}