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.CreateContentCatalogForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.model.control.content.common.ContentCategories;
022import com.echothree.model.control.content.server.control.ContentControl;
023import com.echothree.model.control.offer.server.control.OfferControl;
024import com.echothree.model.control.offer.server.control.OfferUseControl;
025import com.echothree.model.control.offer.server.control.SourceControl;
026import com.echothree.model.control.offer.server.control.UseControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.content.server.entity.ContentCatalog;
031import com.echothree.model.data.offer.server.entity.OfferUse;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045import javax.enterprise.context.RequestScoped;
046
047@RequestScoped
048public class CreateContentCatalogCommand
049        extends BaseSimpleCommand<CreateContentCatalogForm> {
050    
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCatalog.name(), SecurityRoles.Create.name())
059                        )))
060                )));
061        
062        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
069                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
071                ));
072    }
073    
074    /** Creates a new instance of CreateContentCatalogCommand */
075    public CreateContentCatalogCommand() {
076        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
077    }
078    
079    @Override
080    protected BaseResult execute() {
081        var result = ContentResultFactory.getCreateContentCatalogResult();
082        var contentControl = Session.getModelController(ContentControl.class);
083        var contentCollectionName = form.getContentCollectionName();
084        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
085        ContentCatalog contentCatalog = null;
086        
087        if(contentCollection != null) {
088            var contentCatalogName = form.getContentCatalogName();
089            
090            contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
091            
092            if(contentCatalog == null) {
093                var defaultOfferName = form.getDefaultOfferName();
094                var defaultUseName = form.getDefaultUseName();
095                var defaultSourceName = form.getDefaultSourceName();
096                OfferUse defaultOfferUse = null;
097
098                if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) {
099                    var offerControl = Session.getModelController(OfferControl.class);
100                    var defaultOffer = offerControl.getOfferByName(defaultOfferName);
101                    
102                    if(defaultOffer != null) {
103                        var useControl = Session.getModelController(UseControl.class);
104                        var defaultUse = useControl.getUseByName(defaultUseName);
105                        
106                        if(defaultUse != null) {
107                            var offerUseControl = Session.getModelController(OfferUseControl.class);
108                            defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse);
109                            
110                            if(defaultOfferUse == null) {
111                                addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name());
112                            }
113                        }  else {
114                            addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName);
115                        }
116                    } else {
117                        addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName);
118                    }
119                } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) {
120                    var sourceControl = Session.getModelController(SourceControl.class);
121                    var source = sourceControl.getSourceByName(defaultSourceName);
122                    
123                    if(source != null) {
124                        defaultOfferUse = source.getLastDetail().getOfferUse();
125                    } else {
126                        addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName);
127                    }
128                } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName == null) {
129                    defaultOfferUse = contentCollection.getLastDetail().getDefaultOfferUse();
130                } else {
131                    addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name());
132                }
133                
134                if(defaultOfferUse != null) {
135                    var isDefault = Boolean.valueOf(form.getIsDefault());
136                    var sortOrder = Integer.valueOf(form.getSortOrder());
137                    var description = form.getDescription();
138                    var partyPK = getPartyPK();
139                    
140                    contentCatalog = contentControl.createContentCatalog(contentCollection, contentCatalogName, defaultOfferUse,
141                            isDefault, sortOrder, partyPK);
142                    contentControl.createContentCategory(contentCatalog, ContentCategories.ROOT.toString(), null,
143                            defaultOfferUse, null, false, 0, partyPK);
144                    
145                    if(description != null) {
146                        var language = getPreferredLanguage();
147                        
148                        contentControl.createContentCatalogDescription(contentCatalog, language, description, partyPK);
149                    }
150                }
151            } else {
152                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
153            }
154        } else {
155            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
156        }
157
158        if(contentCatalog != null) {
159            var contentCatalogDetail = contentCatalog.getLastDetail();
160
161            result.setContentCollectionName(contentCatalogDetail.getContentCollection().getLastDetail().getContentCollectionName());
162            result.setContentCatalogName(contentCatalogDetail.getContentCatalogName());
163            result.setEntityRef(contentCatalog.getPrimaryKey().getEntityRef());
164        }
165
166        return result;
167    }
168    
169}