001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.ContentEditFactory;
020import com.echothree.control.user.content.common.edit.ContentPageAreaEdit;
021import com.echothree.control.user.content.common.form.EditContentPageAreaForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentPageAreaResult;
024import com.echothree.control.user.content.common.spec.ContentPageAreaSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.content.server.entity.ContentCollection;
031import com.echothree.model.data.content.server.entity.ContentPage;
032import com.echothree.model.data.content.server.entity.ContentPageArea;
033import com.echothree.model.data.content.server.entity.ContentPageAreaBlob;
034import com.echothree.model.data.content.server.entity.ContentPageAreaClob;
035import com.echothree.model.data.content.server.entity.ContentPageAreaDetail;
036import com.echothree.model.data.content.server.entity.ContentPageAreaString;
037import com.echothree.model.data.content.server.entity.ContentPageAreaUrl;
038import com.echothree.model.data.content.server.entity.ContentPageLayout;
039import com.echothree.model.data.content.server.entity.ContentPageLayoutArea;
040import com.echothree.model.data.content.server.entity.ContentSection;
041import com.echothree.model.data.content.server.value.ContentPageAreaDetailValue;
042import com.echothree.model.data.core.server.entity.MimeType;
043import com.echothree.model.data.party.server.entity.Language;
044import com.echothree.model.data.user.common.pk.UserVisitPK;
045import com.echothree.util.common.message.ExecutionErrors;
046import com.echothree.util.common.validation.FieldDefinition;
047import com.echothree.util.common.validation.FieldType;
048import com.echothree.util.common.command.EditMode;
049import com.echothree.util.common.persistence.type.ByteArray;
050import com.echothree.util.server.control.BaseAbstractEditCommand;
051import com.echothree.util.server.control.CommandSecurityDefinition;
052import com.echothree.util.server.control.PartyTypeDefinition;
053import com.echothree.util.server.control.SecurityRoleDefinition;
054import com.echothree.util.server.persistence.Session;
055import java.util.Arrays;
056import java.util.Collections;
057import java.util.List;
058
059public class EditContentPageAreaCommand
060        extends BaseAbstractEditCommand<ContentPageAreaSpec, ContentPageAreaEdit, EditContentPageAreaResult, ContentPageArea, ContentPageArea> {
061    
062    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
063    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
064    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
065    
066    static {
067        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
068                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
069                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
070                        new SecurityRoleDefinition(SecurityRoleGroups.ContentPageArea.name(), SecurityRoles.Edit.name())
071                        )))
072                )));
073        
074        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
075                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
076                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
077                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null),
078                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
079                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
080                ));
081        
082        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
083                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null),
084                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L),
085                new FieldDefinition("ContentPageAreaClob", FieldType.STRING, false, 1L, null),
086                new FieldDefinition("ContentPageAreaUrl", FieldType.URL, false, 1L, 200L)
087                ));
088    }
089    
090    /** Creates a new instance of EditContentPageAreaCommand */
091    public EditContentPageAreaCommand(UserVisitPK userVisitPK, EditContentPageAreaForm form) {
092        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
093    }
094    
095    @Override
096    public EditContentPageAreaResult getResult() {
097        return ContentResultFactory.getEditContentPageAreaResult();
098    }
099    
100    @Override
101    public ContentPageAreaEdit getEdit() {
102        return ContentEditFactory.getContentPageAreaEdit();
103    }
104    
105    @Override
106    public ContentPageArea getEntity(EditContentPageAreaResult result) {
107        var contentControl = Session.getModelController(ContentControl.class);
108        ContentPageArea contentPageArea = null;
109        String contentCollectionName = spec.getContentCollectionName();
110        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
111        
112        if(contentCollection != null) {
113            String contentSectionName = spec.getContentSectionName();
114            ContentSection contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
115            
116            if(contentSection != null) {
117                String contentPageName = spec.getContentPageName();
118                ContentPage contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
119                
120                if(contentPage != null) {
121                    Integer sortOrder = Integer.valueOf(spec.getSortOrder());
122                    ContentPageLayout contentPageLayout = contentPage.getLastDetail().getContentPageLayout();
123                    ContentPageLayoutArea contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder);
124                    
125                    if(contentPageLayoutArea != null) {
126                        var partyControl = Session.getModelController(PartyControl.class);
127                        String languageIsoName = spec.getLanguageIsoName();
128                        Language language = partyControl.getLanguageByIsoName(languageIsoName);
129                        
130                        if(language != null) {
131                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
132                                contentPageArea = contentControl.getContentPageArea(contentPage, contentPageLayoutArea, language);
133                            } else { // EditMode.UPDATE
134                                contentPageArea = contentControl.getContentPageAreaForUpdate(contentPage, contentPageLayoutArea, language);
135                            }
136
137                            if(contentPageArea == null) {
138                                addExecutionError(ExecutionErrors.UnknownContentPageArea.name());
139                            }
140                        } else {
141                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
142                        }
143                    } else {
144                        addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name());
145                    }
146                } else {
147                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
148                }
149            } else {
150                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
151            }
152        } else {
153            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
154        }
155
156        return contentPageArea;
157    }
158    
159    @Override
160    public ContentPageArea getLockEntity(ContentPageArea contentPageArea) {
161        return contentPageArea;
162    }
163    
164    @Override
165    public void fillInResult(EditContentPageAreaResult result, ContentPageArea contentPageArea) {
166        var contentControl = Session.getModelController(ContentControl.class);
167        
168        result.setContentPageArea(contentControl.getContentPageAreaTransfer(getUserVisit(), contentPageArea));
169    }
170    
171    @Override
172    public void doLock(ContentPageAreaEdit edit, ContentPageArea contentPageArea) {
173        var contentControl = Session.getModelController(ContentControl.class);
174        ContentPageAreaDetail contentPageAreaDetail = contentPageArea.getLastDetail();
175        ContentPageAreaBlob contentPageAreaBlob = contentControl.getContentPageAreaBlob(contentPageAreaDetail);
176        ContentPageAreaClob contentPageAreaClob = contentControl.getContentPageAreaClob(contentPageAreaDetail);
177        ContentPageAreaString contentPageAreaString = contentControl.getContentPageAreaString(contentPageAreaDetail);
178        ContentPageAreaUrl contentPageAreaUrl = contentControl.getContentPageAreaUrl(contentPageAreaDetail);
179
180        edit.setMimeTypeName(contentPageArea.getLastDetail().getMimeType().getLastDetail().getMimeTypeName());
181
182        if(contentPageAreaBlob != null) {
183            edit.setContentPageAreaBlob(contentPageAreaBlob.getBlob());
184        }
185
186        if(contentPageAreaClob != null) {
187            edit.setContentPageAreaClob(contentPageAreaClob.getClob());
188        }
189
190        if(contentPageAreaString != null) {
191            edit.setDescription(contentPageAreaString.getString());
192        }
193
194        if(contentPageAreaUrl != null) {
195            edit.setContentPageAreaUrl(contentPageAreaUrl.getUrl());
196        }
197    }
198    
199   MimeType mimeType = null;
200    
201    @Override
202    public void canUpdate(ContentPageArea contentPageArea) {
203        var coreControl = getCoreControl();
204        String mimeTypeName = edit.getMimeTypeName();
205        
206        mimeType = coreControl.getMimeTypeByName(mimeTypeName);
207
208        if(mimeType == null) {
209            addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName);
210        }
211    }
212    
213    @Override
214    public void doUpdate(ContentPageArea contentPageArea) {
215        var contentControl = Session.getModelController(ContentControl.class);
216        ContentPageAreaDetailValue contentPageAreaDetailValue = contentControl.getContentPageAreaDetailValueForUpdate(contentPageArea);
217        String description = edit.getDescription();
218        ByteArray contentPageAreaBlob = edit.getContentPageAreaBlob();
219        String contentPageAreaClob = edit.getContentPageAreaClob();
220        String contentPageAreaUrl = edit.getContentPageAreaUrl();
221
222        contentPageAreaDetailValue.setMimeTypePK(mimeType.getPrimaryKey());
223
224        ContentPageAreaDetail contentPageAreaDetail = contentControl.updateContentPageAreaFromValue(contentPageAreaDetailValue, true, getPartyPK());
225
226        if(contentPageAreaBlob != null) {
227            contentControl.createContentPageAreaBlob(contentPageAreaDetail, contentPageAreaBlob);
228        }
229
230        if(contentPageAreaClob != null) {
231            contentControl.createContentPageAreaClob(contentPageAreaDetail, contentPageAreaClob);
232        }
233
234        if(contentPageAreaUrl != null) {
235            contentControl.createContentPageAreaUrl(contentPageAreaDetail, contentPageAreaUrl);
236        }
237
238        if(description != null) {
239            contentControl.createContentPageAreaString(contentPageAreaDetail, description);
240        }
241    }
242    
243}