001/* 002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 017 * Thomas Roger 018 */ 019 020package org.nuxeo.ecm.platform.publisher.helper; 021 022import org.nuxeo.ecm.core.api.CoreSession; 023import org.nuxeo.ecm.core.api.DocumentModel; 024 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.List; 028 029/** 030 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 031 */ 032public class RootSectionsManager { 033 034 public static final String SCHEMA_PUBLISHING = "publishing"; 035 036 public static final String SECTIONS_PROPERTY_NAME = "publish:sections"; 037 038 protected static final String SECTION_ROOT_DOCUMENT_TYPE = "SectionRoot"; 039 040 protected CoreSession coreSession; 041 042 public RootSectionsManager(CoreSession coreSession) { 043 this.coreSession = coreSession; 044 } 045 046 public boolean canAddSection(DocumentModel section, DocumentModel currentDocument) { 047 if (SECTION_ROOT_DOCUMENT_TYPE.equals(section.getType())) { 048 return false; 049 } 050 String sectionId = section.getId(); 051 if (currentDocument.hasSchema(SCHEMA_PUBLISHING)) { 052 String[] sectionIdsArray = (String[]) currentDocument.getPropertyValue(SECTIONS_PROPERTY_NAME); 053 054 List<String> sectionIdsList = new ArrayList<String>(); 055 056 if (sectionIdsArray != null && sectionIdsArray.length > 0) { 057 sectionIdsList = Arrays.asList(sectionIdsArray); 058 } 059 060 if (sectionIdsList.contains(sectionId)) { 061 return false; 062 } 063 } 064 065 return true; 066 } 067 068 public String addSection(String sectionId, DocumentModel currentDocument) { 069 070 if (sectionId != null && currentDocument.hasSchema(SCHEMA_PUBLISHING)) { 071 String[] sectionIdsArray = (String[]) currentDocument.getPropertyValue(SECTIONS_PROPERTY_NAME); 072 073 List<String> sectionIdsList = new ArrayList<String>(); 074 075 if (sectionIdsArray != null && sectionIdsArray.length > 0) { 076 sectionIdsList = Arrays.asList(sectionIdsArray); 077 // make it resizable 078 sectionIdsList = new ArrayList<String>(sectionIdsList); 079 } 080 081 sectionIdsList.add(sectionId); 082 String[] sectionIdsListIn = new String[sectionIdsList.size()]; 083 sectionIdsList.toArray(sectionIdsListIn); 084 085 currentDocument.setPropertyValue(SECTIONS_PROPERTY_NAME, sectionIdsListIn); 086 coreSession.saveDocument(currentDocument); 087 coreSession.save(); 088 } 089 return null; 090 } 091 092 public String removeSection(String sectionId, DocumentModel currentDocument) { 093 094 if (sectionId != null && currentDocument.hasSchema(SCHEMA_PUBLISHING)) { 095 String[] sectionIdsArray = (String[]) currentDocument.getPropertyValue(SECTIONS_PROPERTY_NAME); 096 097 List<String> sectionIdsList = new ArrayList<String>(); 098 099 if (sectionIdsArray != null && sectionIdsArray.length > 0) { 100 sectionIdsList = Arrays.asList(sectionIdsArray); 101 // make it resizable 102 sectionIdsList = new ArrayList<String>(sectionIdsList); 103 } 104 105 if (!sectionIdsList.isEmpty()) { 106 sectionIdsList.remove(sectionId); 107 108 String[] sectionIdsListIn = new String[sectionIdsList.size()]; 109 sectionIdsList.toArray(sectionIdsListIn); 110 111 currentDocument.setPropertyValue(SECTIONS_PROPERTY_NAME, sectionIdsListIn); 112 coreSession.saveDocument(currentDocument); 113 coreSession.save(); 114 } 115 } 116 117 return null; 118 } 119 120}