001/* 002 * Copyright 2010 Anyware Services 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 */ 016package org.ametys.cms.repository; 017 018import javax.jcr.Node; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022 023import org.ametys.cms.content.ContentHelper; 024import org.ametys.cms.content.ContentSaxer; 025import org.ametys.cms.data.ContentDataHelper; 026import org.ametys.plugins.repository.AmetysObject; 027import org.ametys.plugins.repository.AmetysObjectFactory; 028import org.ametys.plugins.repository.AmetysObjectIterable; 029import org.ametys.plugins.repository.AmetysObjectResolver; 030import org.ametys.plugins.repository.AmetysRepositoryException; 031import org.ametys.plugins.repository.UnknownAmetysObjectException; 032import org.ametys.plugins.repository.jcr.DefaultAmetysObjectFactory; 033import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject; 034import org.ametys.plugins.repository.jcr.TraversableAmetysObjectHelper; 035 036/** 037 * {@link AmetysObjectFactory} handling {@link DefaultContent}. 038 */ 039public class ContentFactory extends DefaultAmetysObjectFactory 040{ 041 private ServiceManager _serviceManager; 042 private ContentDAO _contentDAO; 043 private ContentSaxer _contentSaxer; 044 private ContentHelper _contentHelper; 045 private ModifiableContentHelper _modifiableContentHelper; 046 private ContentDataHelper _contentDataHelper; 047 048 @Override 049 public void service(ServiceManager smanager) throws ServiceException 050 { 051 super.service(smanager); 052 _serviceManager = smanager; 053 _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE); 054 _modifiableContentHelper = (ModifiableContentHelper) smanager.lookup(ModifiableContentHelper.ROLE); 055 _contentSaxer = (ContentSaxer) smanager.lookup(ContentSaxer.CMS_CONTENT_SAXER_ROLE); 056 _contentDataHelper = (ContentDataHelper) smanager.lookup(ContentDataHelper.ROLE); 057 } 058 059 synchronized ContentDAO _getContentDAO() 060 { 061 // lazy initialization to prevent chicken/egg scenario on startup 062 if (_contentDAO == null) 063 { 064 try 065 { 066 _contentDAO = (ContentDAO) _serviceManager.lookup(ContentDAO.ROLE); 067 } 068 catch (ServiceException e) 069 { 070 throw new AmetysRepositoryException("Caught an exception while retrieving ContentDAO", e); 071 } 072 } 073 return _contentDAO; 074 } 075 076 AmetysObjectResolver _getAOResolver() 077 { 078 return _resolver; 079 } 080 081 ModifiableContentHelper _getModifiableContentHelper() 082 { 083 return _modifiableContentHelper; 084 } 085 086 /** 087 * Retrieves the content saxer 088 * @return the content saxer 089 */ 090 public ContentSaxer getContentSaxer() 091 { 092 return _contentSaxer; 093 } 094 095 /** 096 * Retrieves the content helper 097 * @return the content helper 098 */ 099 public ContentHelper getContentHelper() 100 { 101 return _contentHelper; 102 } 103 104 /** 105 * Retrieves the content data helper 106 * @return the content data helper 107 */ 108 public ContentDataHelper getContentDataHelper() 109 { 110 return _contentDataHelper; 111 } 112 113 @Override 114 public DefaultContent getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException 115 { 116 return new DefaultContent<>(node, parentPath, this); 117 } 118 119 /** 120 * Returns the {@link AmetysObject} at the given subPath, 121 * relative to the given {@link DefaultTraversableAmetysObject}. 122 * @param <A> the actual type of {@link AmetysObject}. 123 * @param object the context {@link DefaultTraversableAmetysObject}. 124 * @param path the sub path. Cannot be <code>null</code>, empty or absolute. 125 * @return the {@link AmetysObject} at the given subPath, 126 * relative to the given {@link DefaultTraversableAmetysObject}. 127 * @throws AmetysRepositoryException if an error occurs. 128 * @throws UnknownAmetysObjectException if no such object exists. 129 */ 130 public <A extends AmetysObject> A getChild(DefaultContent object, String path) throws AmetysRepositoryException, UnknownAmetysObjectException 131 { 132 return TraversableAmetysObjectHelper.<A>getChild(object, this, path, _resolver, getLogger()); 133 } 134 135 /** 136 * Returns all children of the given {@link DefaultTraversableAmetysObject}. 137 * @param <A> the actual type of {@link AmetysObject}s 138 * @param object a {@link DefaultTraversableAmetysObject}. 139 * @return a List containing all children object in the Ametys hierarchy. 140 * @throws AmetysRepositoryException if an error occurs. 141 */ 142 public <A extends AmetysObject> AmetysObjectIterable<A> getChildren(DefaultContent object) throws AmetysRepositoryException 143 { 144 return TraversableAmetysObjectHelper.getChildren(object, this, _resolver, getLogger()); 145 } 146 147 /** 148 * Tests if a given object has a child with a given name. 149 * @param object the context object. 150 * @param name the name to test. 151 * @return <code>true</code> is the given object has a child with the given name, 152 * <code>false</code> otherwise. 153 * @throws AmetysRepositoryException if an error occurs. 154 */ 155 public boolean hasChild(DefaultContent object, String name) throws AmetysRepositoryException 156 { 157 return TraversableAmetysObjectHelper.hasChild(object, name, _ametysFactoryExtensionPoint, getLogger()); 158 } 159 160 /** 161 * Creates a child to the given object. 162 * @param <A> the actual type of {@link AmetysObject}. 163 * @param object the parent {@link AmetysObject}. 164 * @param name the new object's name. 165 * @param type the new object's type. 166 * @return the newly created {@link AmetysObject}. 167 * @throws AmetysRepositoryException if an error occurs. 168 */ 169 public <A extends AmetysObject> A createChild(DefaultContent object, String name, String type) throws AmetysRepositoryException 170 { 171 return TraversableAmetysObjectHelper.<A>createChild(object, this, name, type, _ametysFactoryExtensionPoint, _resolver, getLogger()); 172 } 173}