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.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 com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collection;
035import java.util.Collections;
036import java.util.List;
037import javax.enterprise.context.RequestScoped;
038
039@RequestScoped
040public class GetContentCatalogsCommand
041        extends BasePaginatedMultipleEntitiesCommand<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() {
058        super(null, FORM_FIELD_DEFINITIONS, true);
059    }
060    
061    ContentCollection contentCollection;
062
063    @Override
064    protected void handleForm() {
065        var contentWebAddressName = form.getContentWebAddressName();
066        var contentCollectionName = form.getContentCollectionName();
067        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
068
069        if(parameterCount == 1) {
070            var contentControl = Session.getModelController(ContentControl.class);
071
072            if(contentWebAddressName != null) {
073                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
074
075                if(contentWebAddress != null) {
076                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
077                } else {
078                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
079                }
080            } else {
081                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
082
083                if(contentCollection == null) {
084                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
085                }
086            }
087        } else {
088            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
089        }
090
091        if(!hasExecutionErrors()) {
092            AssociateReferralLogic.getInstance().handleAssociateReferral(session, this, form, getUserVisitForUpdate(),
093                    contentCollection.getPrimaryKey(), getPartyPK());
094        }
095    }
096
097    @Override
098    protected Long getTotalEntities() {
099        var contentControl = Session.getModelController(ContentControl.class);
100
101        return hasExecutionErrors() ? null :
102                contentControl.countContentCatalogsByContentCollection(contentCollection);
103    }
104
105    @Override
106    protected Collection<ContentCatalog> getEntities() {
107        Collection<ContentCatalog> contentCatalogs = null;
108
109        if(!hasExecutionErrors()) {
110            var contentControl = Session.getModelController(ContentControl.class);
111
112            contentCatalogs = contentControl.getContentCatalogs(contentCollection);
113        }
114        
115        return contentCatalogs;
116    }
117    
118    @Override
119    protected BaseResult getResult(Collection<ContentCatalog> entities) {
120        var result = ContentResultFactory.getGetContentCatalogsResult();
121        
122        if(entities != null) {
123            var contentControl = Session.getModelController(ContentControl.class);
124            var userVisit = getUserVisit();
125
126            if(session.hasLimit(ContentCatalogFactory.class)) {
127                result.setContentCatalogCount(getTotalEntities());
128            }
129
130            result.setContentCollection(contentControl.getContentCollectionTransfer(userVisit, contentCollection));
131            result.setContentCatalogs(contentControl.getContentCatalogTransfers(userVisit, entities));
132        }
133        
134        return result;
135    }
136
137}