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.GetContentCatalogItemsForm;
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.party.common.PartyTypes;
024import com.echothree.model.control.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.data.content.server.entity.ContentCatalog;
027import com.echothree.model.data.content.server.entity.ContentCatalogItem;
028import com.echothree.model.data.content.server.entity.ContentCollection;
029import com.echothree.model.data.content.server.factory.ContentCatalogItemFactory;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.Collection;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
044
045@Dependent
046public class GetContentCatalogItemsCommand
047        extends BasePaginatedMultipleEntitiesCommand<ContentCatalogItem, GetContentCatalogItemsForm> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCatalogItem.name(), SecurityRoles.List.name())
057                ))
058        ));
059        
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("ContentWebAddressName", FieldType.HOST_NAME, false, null, null),
062                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("AssociateProgramName", FieldType.STRING, false, null, null),
065                new FieldDefinition("AssociateName", FieldType.STRING, false, null, null),
066                new FieldDefinition("AssociatePartyContactMechanismName", FieldType.STRING, false, null, null)
067        );
068    }
069
070    @Inject
071    ContentControl contentControl;
072
073    @Inject
074    AssociateReferralLogic associateReferralLogic;
075
076    
077    /** Creates a new instance of GetContentCatalogItemsCommand */
078    public GetContentCatalogItemsCommand() {
079        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
080    }
081    
082    private ContentCatalog contentCatalog;
083
084    @Override
085    protected void handleForm() {
086        var contentWebAddressName = form.getContentWebAddressName();
087        var contentCollectionName = form.getContentCollectionName();
088        var parameterCount = (contentWebAddressName == null ? 0 : 1) + (contentCollectionName == null ? 0 : 1);
089
090        if(parameterCount == 1) {
091            ContentCollection contentCollection = null;
092
093            if(contentWebAddressName != null) {
094                var contentWebAddress = contentControl.getContentWebAddressByName(contentWebAddressName);
095
096                if(contentWebAddress != null) {
097                    contentCollection = contentWebAddress.getLastDetail().getContentCollection();
098                } else {
099                    addExecutionError(ExecutionErrors.UnknownContentWebAddressName.name(), contentWebAddressName);
100                }
101            } else {
102                contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
103
104                if(contentCollection == null) {
105                    addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
106                }
107            }
108
109            if(!hasExecutionErrors()) {
110                var contentCatalogName = form.getContentCatalogName();
111
112                if(contentCatalogName == null) {
113                    contentCatalog = contentControl.getDefaultContentCatalog(contentCollection);
114
115                    if(contentCatalog == null) {
116                        addExecutionError(ExecutionErrors.UnknownDefaultContentCatalog.name(),
117                                contentCollection.getLastDetail().getContentCollectionName());
118                    }
119                } else {
120                    contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
121
122                    if(contentCatalog == null) {
123                        addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(),
124                                contentCollection.getLastDetail().getContentCollectionName(), contentCatalogName);
125                    }
126                }
127            }
128        } else {
129            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
130        }
131
132        if(!hasExecutionErrors()) {
133            associateReferralLogic.handleAssociateReferral(session, this, form, getUserVisitForUpdate(),
134                    contentCatalog.getPrimaryKey(), getPartyPK());
135
136        }
137    }
138
139    @Override
140    protected Long getTotalEntities() {
141        return hasExecutionErrors() ? null :
142                contentControl.countContentCatalogItemsByContentCatalog(contentCatalog);
143    }
144
145    @Override
146    protected Collection<ContentCatalogItem> getEntities() {
147        Collection<ContentCatalogItem> contentCatalogItems = null;
148
149        if(!hasExecutionErrors()) {
150            contentCatalogItems = contentControl.getContentCatalogItemsByContentCatalog(contentCatalog);
151        }
152
153        return contentCatalogItems;
154    }
155    
156    @Override
157    protected BaseResult getResult(Collection<ContentCatalogItem> entities) {
158        var result = ContentResultFactory.getGetContentCatalogItemsResult();
159
160        if(entities != null) {
161            var userVisit = getUserVisit();
162
163            result.setContentCatalog(contentControl.getContentCatalogTransfer(userVisit, contentCatalog));
164
165            if(session.hasLimit(ContentCatalogItemFactory.class)) {
166                result.setContentCatalogItemCount(getTotalEntities());
167            }
168
169            result.setContentCatalogItems(contentControl.getContentCatalogItemTransfers(userVisit, entities));
170        }
171        
172        return result;
173    }
174    
175}