001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.file.collectors; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.lock.CmsLockUtil; 033import org.opencms.main.CmsException; 034import org.opencms.relations.CmsCategory; 035import org.opencms.relations.CmsCategoryService; 036 037import java.util.Arrays; 038import java.util.List; 039 040/** 041 * A post create handler that adds categories to newly created resources (that are not a copy of an existing resource).<p> 042 * 043 * To configure it for adding the categories with category paths "cat1/subcat1/", "cat1/subcat2/" and "cat2/" 044 * add the attribute 045 * <code>postCreateHandler="org.opencms.file.collectors.CmsAddCategoriesPostCreateHandler|cat1/subcat1/,cat1/subcat2/,cat2/"</code> 046 * to the tag that provides the create option (i.e., <cms:contentload>, <cms:edit> or <cms:display>.<p> 047 * 048 * Instead of providing category paths, one can also use site or root paths of the folders representing the categories. 049 * If a category path starts with "/" it is assumed to be site or root path. Otherwise it is treated as category path, i.e., 050 * the path without the category repositories base path.<p> 051 * 052 * @see I_CmsCollectorPostCreateHandler for more information on where post create handlers can be configured. 053 */ 054public class CmsAddCategoriesPostCreateHandler implements I_CmsCollectorPostCreateHandler { 055 056 /** 057 * @see org.opencms.file.collectors.I_CmsCollectorPostCreateHandler#onCreate(org.opencms.file.CmsObject, org.opencms.file.CmsResource, boolean) 058 */ 059 public void onCreate(CmsObject cms, CmsResource createdResource, boolean copyMode) { 060 061 // there are no categories configured that should be added. Just return without modifying the created resource. 062 return; 063 064 } 065 066 /** 067 * Adds the categories specified via <code>config</code> to the newly created resource iff not in copy mode. 068 * 069 * @param cms the current user's CMS context 070 * @param createdResource the resource which has been created 071 * @param copyMode <code>true</code> if the user chose one of the elements in the collector list as a model 072 * @param config a comma separted list of category, site or root paths that specify the categories to be added to the created resource 073 * if <code>copyMode</code> is <code>false</code>. 074 * 075 * @see org.opencms.file.collectors.I_CmsCollectorPostCreateHandler#onCreate(org.opencms.file.CmsObject, org.opencms.file.CmsResource, boolean, java.lang.String) 076 */ 077 public void onCreate(CmsObject cms, CmsResource createdResource, boolean copyMode, String config) { 078 079 if ((null != config) && !copyMode) { 080 List<String> cats = Arrays.asList(config.split(",")); 081 CmsCategoryService catService = CmsCategoryService.getInstance(); 082 try { 083 try (AutoCloseable c = CmsLockUtil.withLockedResources(cms, createdResource)) { 084 String sitePath = cms.getRequestContext().getSitePath(createdResource); 085 for (String catPath : cats) { 086 if (!catPath.isEmpty()) { 087 try { 088 CmsCategory cat; 089 if (catPath.startsWith("/")) { // assume we have a site or rootpath 090 cat = catService.getCategory(cms, catPath); 091 } else { // assume we have the category path 092 cat = catService.readCategory(cms, catPath, sitePath); 093 } 094 if (null != cat) { 095 catService.addResourceToCategory(cms, sitePath, cat); 096 } 097 } catch (CmsException e) { 098 // TODO Auto-generated catch block 099 e.printStackTrace(); 100 } 101 } 102 } 103 } 104 } catch (Exception e1) { 105 // TODO Auto-generated catch block 106 e1.printStackTrace(); 107 } 108 } 109 } 110 111}