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