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.edit.ContentCatalogEdit;
020import com.echothree.control.user.content.common.edit.ContentEditFactory;
021import com.echothree.control.user.content.common.form.EditContentCatalogForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentCatalogResult;
024import com.echothree.control.user.content.common.spec.ContentCatalogSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.offer.server.control.OfferControl;
027import com.echothree.model.control.offer.server.control.OfferUseControl;
028import com.echothree.model.control.offer.server.control.SourceControl;
029import com.echothree.model.control.offer.server.control.UseControl;
030import com.echothree.model.control.party.common.PartyTypes;
031import com.echothree.model.control.party.server.control.PartyControl;
032import com.echothree.model.control.security.common.SecurityRoleGroups;
033import com.echothree.model.control.security.common.SecurityRoles;
034import com.echothree.model.data.content.server.entity.ContentCatalog;
035import com.echothree.model.data.content.server.entity.ContentCollection;
036import com.echothree.model.data.offer.server.entity.OfferUse;
037import com.echothree.model.data.user.common.pk.UserVisitPK;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.common.message.ExecutionErrors;
040import com.echothree.util.common.validation.FieldDefinition;
041import com.echothree.util.common.validation.FieldType;
042import com.echothree.util.server.control.BaseAbstractEditCommand;
043import com.echothree.util.server.control.CommandSecurityDefinition;
044import com.echothree.util.server.control.PartyTypeDefinition;
045import com.echothree.util.server.control.SecurityRoleDefinition;
046import com.echothree.util.server.persistence.Session;
047import java.util.List;
048import javax.enterprise.context.Dependent;
049
050@Dependent
051public class EditContentCatalogCommand
052        extends BaseAbstractEditCommand<ContentCatalogSpec, ContentCatalogEdit, EditContentCatalogResult, ContentCatalog, ContentCatalog> {
053    
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
056    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
057    
058    static {
059        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
060                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
061                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
062                        new SecurityRoleDefinition(SecurityRoleGroups.ContentCatalog.name(), SecurityRoles.Edit.name())
063                        ))
064                ));
065        
066        SPEC_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null)
069                );
070        
071        EDIT_FIELD_DEFINITIONS = List.of(
072                new FieldDefinition("ContentCatalogName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("DefaultOfferName", FieldType.ENTITY_NAME, false, null, null),
074                new FieldDefinition("DefaultUseName", FieldType.ENTITY_NAME, false, null, null),
075                new FieldDefinition("DefaultSourceName", FieldType.ENTITY_NAME, false, null, null),
076                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
077                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
078                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
079                );
080    }
081    
082    /** Creates a new instance of EditContentCatalogCommand */
083    public EditContentCatalogCommand() {
084        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
085    }
086    
087    @Override
088    public EditContentCatalogResult getResult() {
089        return ContentResultFactory.getEditContentCatalogResult();
090    }
091    
092    @Override
093    public ContentCatalogEdit getEdit() {
094        return ContentEditFactory.getContentCatalogEdit();
095    }
096    
097    ContentCollection contentCollection = null;
098    
099    @Override
100    public ContentCatalog getEntity(EditContentCatalogResult result) {
101        var contentControl = Session.getModelController(ContentControl.class);
102        ContentCatalog contentCatalog = null;
103        var contentCollectionName = spec.getContentCollectionName();
104        
105        contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
106        
107        if(contentCollection != null) {
108            var contentCatalogName = spec.getContentCatalogName();
109
110            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
111                contentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
112            } else { // EditMode.UPDATE
113                contentCatalog = contentControl.getContentCatalogByNameForUpdate(contentCollection, contentCatalogName);
114            }
115
116            if(contentCatalog != null) {
117                result.setContentCatalog(contentControl.getContentCatalogTransfer(getUserVisit(), contentCatalog));
118            } else {
119                addExecutionError(ExecutionErrors.UnknownContentCatalogName.name(), contentCatalogName);
120            }
121        } else {
122            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
123        }
124
125        return contentCatalog;
126    }
127    
128    @Override
129    public ContentCatalog getLockEntity(ContentCatalog contentCatalog) {
130        return contentCatalog;
131    }
132    
133    @Override
134    public void fillInResult(EditContentCatalogResult result, ContentCatalog contentCatalog) {
135        var contentControl = Session.getModelController(ContentControl.class);
136        
137        result.setContentCatalog(contentControl.getContentCatalogTransfer(getUserVisit(), contentCatalog));
138    }
139    
140    @Override
141    public void doLock(ContentCatalogEdit edit, ContentCatalog contentCatalog) {
142        var contentControl = Session.getModelController(ContentControl.class);
143        var sourceControl = Session.getModelController(SourceControl.class);
144        var contentCatalogDescription = contentControl.getContentCatalogDescription(contentCatalog, getPreferredLanguage());
145        var contentCatalogDetail = contentCatalog.getLastDetail();
146        var offerUse = contentCatalogDetail.getDefaultOfferUse();
147        var defaultOfferUseDetail = offerUse.getLastDetail();
148        var sources = defaultOfferUse == null ? null : sourceControl.getSourcesByOfferUse(defaultOfferUse); // List of Sources if there are any for the OfferUse
149        var defaultSourceName = sources == null ? null : sources.iterator().next().getLastDetail().getSourceName(); // From the List, the first available Source's SourceName
150
151        edit.setContentCatalogName(contentCatalogDetail.getContentCatalogName());
152        edit.setDefaultOfferName(defaultSourceName == null ? (defaultOfferUseDetail == null? null: defaultOfferUseDetail.getOffer().getLastDetail().getOfferName()) : null);
153        edit.setDefaultUseName(defaultSourceName == null ? (defaultOfferUseDetail == null ? null : defaultOfferUseDetail.getUse().getLastDetail().getUseName()) : null);
154        edit.setDefaultSourceName(defaultSourceName);
155        edit.setIsDefault(contentCatalogDetail.getIsDefault().toString());
156        edit.setSortOrder(contentCatalogDetail.getSortOrder().toString());
157
158        if(contentCatalogDescription != null) {
159            edit.setDescription(contentCatalogDescription.getDescription());
160        }
161    }
162    
163    OfferUse defaultOfferUse = null;
164    
165    @Override
166    public void canUpdate(ContentCatalog contentCatalog) {
167        var contentControl = Session.getModelController(ContentControl.class);
168        var contentCatalogName = edit.getContentCatalogName();
169        var duplicateContentCatalog = contentControl.getContentCatalogByName(contentCollection, contentCatalogName);
170
171        if(duplicateContentCatalog == null || contentCatalog.equals(duplicateContentCatalog)) {
172            var offerControl = Session.getModelController(OfferControl.class);
173            var defaultOfferName = edit.getDefaultOfferName();
174            var defaultUseName = edit.getDefaultUseName();
175            var defaultSourceName = edit.getDefaultSourceName();
176
177            if(defaultOfferName != null && defaultUseName != null && defaultSourceName == null) {
178                var defaultOffer = offerControl.getOfferByName(defaultOfferName);
179
180                if(defaultOffer != null) {
181                    var useControl = Session.getModelController(UseControl.class);
182                    var defaultUse = useControl.getUseByName(defaultUseName);
183
184                    if(defaultUse != null) {
185                        var offerUseControl = Session.getModelController(OfferUseControl.class);
186                        defaultOfferUse = offerUseControl.getOfferUse(defaultOffer, defaultUse);
187
188                        if(defaultOfferUse == null) {
189                            addExecutionError(ExecutionErrors.UnknownDefaultOfferUse.name());
190                        }
191                    }  else {
192                        addExecutionError(ExecutionErrors.UnknownDefaultUseName.name(), defaultUseName);
193                    }
194                } else {
195                    addExecutionError(ExecutionErrors.UnknownDefaultOfferName.name(), defaultOfferName);
196                }
197            } else if(defaultOfferName == null && defaultUseName == null && defaultSourceName != null) {
198                var sourceControl = Session.getModelController(SourceControl.class);
199                var source = sourceControl.getSourceByName(defaultSourceName);
200
201                if(source != null) {
202                    defaultOfferUse = source.getLastDetail().getOfferUse();
203                } else {
204                    addExecutionError(ExecutionErrors.UnknownDefaultSourceName.name(), defaultSourceName);
205                }
206            } else {
207                addExecutionError(ExecutionErrors.InvalidDefaultOfferOrSourceSpecification.name());
208            }
209
210            if(defaultOfferUse != null) {
211                var partyControl = Session.getModelController(PartyControl.class);
212                var defaultOffer = defaultOfferUse.getLastDetail().getOffer();
213                var defaultPartyDepartment = partyControl.getPartyDepartment(defaultOffer.getLastDetail().getDepartmentParty());
214                var defaultPartyDivision = partyControl.getPartyDivision(defaultPartyDepartment.getDivisionParty());
215                var defaultPartyCompany = partyControl.getPartyCompany(defaultPartyDivision.getCompanyParty());
216
217                var catalogOffer = contentCatalog.getLastDetail().getDefaultOfferUse().getLastDetail().getOffer();
218                var catalogPartyDepartment = partyControl.getPartyDepartment(catalogOffer.getLastDetail().getDepartmentParty());
219                var catalogPartyDivision = partyControl.getPartyDivision(catalogPartyDepartment.getDivisionParty());
220                var catalogPartyCompany = partyControl.getPartyCompany(catalogPartyDivision.getCompanyParty());
221
222                if(!defaultPartyCompany.equals(catalogPartyCompany)) {
223                    addExecutionError(ExecutionErrors.InvalidOfferCompany.name());
224                }
225            }
226        } else {
227            addExecutionError(ExecutionErrors.DuplicateContentCatalogName.name(), contentCatalogName);
228        }
229    }
230    
231    @Override
232    public void doUpdate(ContentCatalog contentCatalog) {
233        var contentControl = Session.getModelController(ContentControl.class);
234        var partyPK = getPartyPK();
235        var contentCatalogDetailValue = contentControl.getContentCatalogDetailValueForUpdate(contentCatalog);
236        var contentCatalogDescription = contentControl.getContentCatalogDescriptionForUpdate(contentCatalog, getPreferredLanguage());
237        var description = edit.getDescription();
238
239        contentCatalogDetailValue.setContentCatalogName(edit.getContentCatalogName());
240        contentCatalogDetailValue.setDefaultOfferUsePK(defaultOfferUse.getPrimaryKey());
241        contentCatalogDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
242        contentCatalogDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
243
244        contentControl.updateContentCatalogFromValue(contentCatalogDetailValue, partyPK);
245
246        if(contentCatalogDescription == null && description != null) {
247            contentControl.createContentCatalogDescription(contentCatalog, getPreferredLanguage(), description, partyPK);
248        } else if(contentCatalogDescription != null && description == null) {
249            contentControl.deleteContentCatalogDescription(contentCatalogDescription, partyPK);
250        } else if(contentCatalogDescription != null && description != null) {
251            var contentCatalogDescriptionValue = contentControl.getContentCatalogDescriptionValue(contentCatalogDescription);
252
253            contentCatalogDescriptionValue.setDescription(description);
254            contentControl.updateContentCatalogDescriptionFromValue(contentCatalogDescriptionValue, partyPK);
255        }
256    }
257    
258}
259