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.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 java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
044
045@Dependent
046public class CreateContentCatalogCommand
047        extends BaseSimpleCommand<CreateContentCatalogForm> {
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.ContentCatalog.name(), SecurityRoles.Create.name())
057                ))
058        ));
059        
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
067                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
068                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
069        );
070    }
071
072    @Inject
073    ContentControl contentControl;
074
075    @Inject
076    OfferControl offerControl;
077
078    @Inject
079    OfferUseControl offerUseControl;
080
081    @Inject
082    SourceControl sourceControl;
083
084    @Inject
085    UseControl useControl;
086
087    
088    /** Creates a new instance of CreateContentCatalogCommand */
089    public CreateContentCatalogCommand() {
090        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
091    }
092    
093    @Override
094    protected BaseResult execute() {
095        var result = ContentResultFactory.getCreateContentCatalogResult();
096        var contentCollectionName = form.getContentCollectionName();
097        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
098        ContentCatalog contentCatalog = null;
099        
100        if(contentCollection != null) {
101            var contentCatalogName = form.getContentCatalogName();
102            
103            contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
104            
105            if(contentCatalog == null) {
106                var defaultOfferName = form.getDefaultOfferName();
107                var defaultUseName = form.getDefaultUseName();
108                var defaultSourceName = form.getDefaultSourceName();
109                OfferUse defaultOfferUse = null;
110
111                if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) {
112                    var defaultOffer = offerControl.getOfferByName(defaultOfferName);
113                    
114                    if(defaultOffer != null) {
115                        var defaultUse = useControl.getUseByName(defaultUseName);
116                        
117                        if(defaultUse != null) {
118                            defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse);
119                            
120                            if(defaultOfferUse == null) {
121                                addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name());
122                            }
123                        }  else {
124                            addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName);
125                        }
126                    } else {
127                        addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName);
128                    }
129                } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) {
130                    var source = sourceControl.getSourceByName(defaultSourceName);
131                    
132                    if(source != null) {
133                        defaultOfferUse = source.getLastDetail().getOfferUse();
134                    } else {
135                        addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName);
136                    }
137                } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName == null) {
138                    defaultOfferUse = contentCollection.getLastDetail().getDefaultOfferUse();
139                } else {
140                    addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name());
141                }
142                
143                if(defaultOfferUse != null) {
144                    var isDefault = Boolean.valueOf(form.getIsDefault());
145                    var sortOrder = Integer.valueOf(form.getSortOrder());
146                    var description = form.getDescription();
147                    var partyPK = getPartyPK();
148                    
149                    contentCatalog = contentControl.createContentCatalog(contentCollection, contentCatalogName, defaultOfferUse,
150                            isDefault, sortOrder, partyPK);
151                    contentControl.createContentCategory(contentCatalog, ContentCategories.ROOT.toString(), null,
152                            defaultOfferUse, null, false, 0, partyPK);
153                    
154                    if(description != null) {
155                        var language = getPreferredLanguage();
156                        
157                        contentControl.createContentCatalogDescription(contentCatalog, language, description, partyPK);
158                    }
159                }
160            } else {
161                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
162            }
163        } else {
164            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
165        }
166
167        if(contentCatalog != null) {
168            var contentCatalogDetail = contentCatalog.getLastDetail();
169
170            result.setContentCollectionName(contentCatalogDetail.getContentCollection().getLastDetail().getContentCollectionName());
171            result.setContentCatalogName(contentCatalogDetail.getContentCatalogName());
172            result.setEntityRef(contentCatalog.getPrimaryKey().getEntityRef());
173        }
174
175        return result;
176    }
177    
178}