001/*
002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.platform.publisher.helper;
019
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentModel;
022
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026
027/**
028 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
029 */
030public class RootSectionsManager {
031
032    public static final String SCHEMA_PUBLISHING = "publishing";
033
034    public static final String SECTIONS_PROPERTY_NAME = "publish:sections";
035
036    protected static final String SECTION_ROOT_DOCUMENT_TYPE = "SectionRoot";
037
038    protected CoreSession coreSession;
039
040    public RootSectionsManager(CoreSession coreSession) {
041        this.coreSession = coreSession;
042    }
043
044    public boolean canAddSection(DocumentModel section, DocumentModel currentDocument) {
045        if (SECTION_ROOT_DOCUMENT_TYPE.equals(section.getType())) {
046            return false;
047        }
048        String sectionId = section.getId();
049        if (currentDocument.hasSchema(SCHEMA_PUBLISHING)) {
050            String[] sectionIdsArray = (String[]) currentDocument.getPropertyValue(SECTIONS_PROPERTY_NAME);
051
052            List<String> sectionIdsList = new ArrayList<String>();
053
054            if (sectionIdsArray != null && sectionIdsArray.length > 0) {
055                sectionIdsList = Arrays.asList(sectionIdsArray);
056            }
057
058            if (sectionIdsList.contains(sectionId)) {
059                return false;
060            }
061        }
062
063        return true;
064    }
065
066    public String addSection(String sectionId, DocumentModel currentDocument) {
067
068        if (sectionId != null && currentDocument.hasSchema(SCHEMA_PUBLISHING)) {
069            String[] sectionIdsArray = (String[]) currentDocument.getPropertyValue(SECTIONS_PROPERTY_NAME);
070
071            List<String> sectionIdsList = new ArrayList<String>();
072
073            if (sectionIdsArray != null && sectionIdsArray.length > 0) {
074                sectionIdsList = Arrays.asList(sectionIdsArray);
075                // make it resizable
076                sectionIdsList = new ArrayList<String>(sectionIdsList);
077            }
078
079            sectionIdsList.add(sectionId);
080            String[] sectionIdsListIn = new String[sectionIdsList.size()];
081            sectionIdsList.toArray(sectionIdsListIn);
082
083            currentDocument.setPropertyValue(SECTIONS_PROPERTY_NAME, sectionIdsListIn);
084            coreSession.saveDocument(currentDocument);
085            coreSession.save();
086        }
087        return null;
088    }
089
090    public String removeSection(String sectionId, DocumentModel currentDocument) {
091
092        if (sectionId != null && currentDocument.hasSchema(SCHEMA_PUBLISHING)) {
093            String[] sectionIdsArray = (String[]) currentDocument.getPropertyValue(SECTIONS_PROPERTY_NAME);
094
095            List<String> sectionIdsList = new ArrayList<String>();
096
097            if (sectionIdsArray != null && sectionIdsArray.length > 0) {
098                sectionIdsList = Arrays.asList(sectionIdsArray);
099                // make it resizable
100                sectionIdsList = new ArrayList<String>(sectionIdsList);
101            }
102
103            if (!sectionIdsList.isEmpty()) {
104                sectionIdsList.remove(sectionId);
105
106                String[] sectionIdsListIn = new String[sectionIdsList.size()];
107                sectionIdsList.toArray(sectionIdsListIn);
108
109                currentDocument.setPropertyValue(SECTIONS_PROPERTY_NAME, sectionIdsListIn);
110                coreSession.saveDocument(currentDocument);
111                coreSession.save();
112            }
113        }
114
115        return null;
116    }
117
118}