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