Index: objects/classes/DBListClass.java =================================================================== --- objects/classes/DBListClass.java (revision 3012) +++ objects/classes/DBListClass.java (working copy) @@ -73,10 +73,10 @@ return new ArrayList(); try { - if ((xwiki.getHibernateStore()!=null)&&(!query.startsWith("/"))) - return makeList(xwiki.search(query, context)); - else - return makeList(((QueryPlugin)xwiki.getPlugin("query", context)).xpath(query).list()); + if (!query.startsWith("/")) + return makeList(xwiki.search(query, context)); + else + return makeList(((QueryPlugin)xwiki.getPlugin("query", context)).xpath(query).list()); } catch (Exception e) { e.printStackTrace(); return new ArrayList(); @@ -116,42 +116,42 @@ } catch (Exception e) { e.printStackTrace(); } + if ((sql==null)||(sql.trim().equals(""))) { String classname = getClassname(); String idField = getIdField(); String valueField = getValueField(); if ((valueField==null)||(valueField.trim().equals(""))) - valueField = idField; - if (context.getWiki().getHibernateStore()!=null) { - String select = "select "; - String tables = " from XWikiDocument as doc, BaseObject as obj"; - String where = " where doc.fullName=obj.name and obj.className='" + classname + "'"; - if (idField.startsWith("doc.")||idField.startsWith("obj.")) - select += idField + ","; - else { - select += "idprop.value,"; - tables += ", StringProperty as idprop"; - where += " and obj.id=idprop.id.id and idprop.id.name='" + idField + "'"; - } - if (valueField.startsWith("doc.")||valueField.startsWith("obj.")) - select += valueField + ","; - else { - if (idField.equals(valueField)) { - select += "idprop.value,"; - } else { + valueField = idField; + + // TODO: query plugin impl. + // We need to generate the right query for the query plugin + + String select = "select "; + String tables = " from XWikiDocument as doc, BaseObject as obj"; + String where = " where doc.fullName=obj.name and obj.className='" + classname + "'"; + if (idField.startsWith("doc.")||idField.startsWith("obj.")) + select += idField + ","; + else { + select += "idprop.value,"; + tables += ", StringProperty as idprop"; + where += " and obj.id=idprop.id.id and idprop.id.name='" + idField + "'"; + } + if (valueField.startsWith("doc.")||valueField.startsWith("obj.")) + select += valueField + ","; + else { + if (idField.equals(valueField)) { + select += "idprop.value,"; + } else { select += "valueprop.value,"; tables += ", StringProperty as valueprop"; where += " and obj.id=valueprop.id.id and valueprop.id.name='" + valueField + "'"; - } } - // Let's create the sql - sql = select + tables + where; - } else { - // TODO: query plugin impl. - // We need to generate the right query for the query plugin } + // Let's create the sql + sql = select + tables + where; + } - } return context.getWiki().parseContent(sql, context); } Index: objects/classes/DBTreeListClass.java =================================================================== --- objects/classes/DBTreeListClass.java (revision 3012) +++ objects/classes/DBTreeListClass.java (working copy) @@ -287,52 +287,50 @@ String idField = getIdField(); String valueField = getValueField(); String parentField = getParentField(); + + // TODO: query plugin impl. + // We need to generate the right query for the query plugin + if ((valueField==null)||(valueField.trim().equals(""))) - valueField = idField; - if (context.getWiki().getHibernateStore()!=null) { - String select = "select "; - String tables = " from XWikiDocument as doc, BaseObject as obj"; - String where = " where doc.fullName=obj.name and obj.className='" + classname + "'"; - if (idField.startsWith("doc.")||idField.startsWith("obj.")) - select += idField + ","; - else { + valueField = idField; + String select = "select "; + String tables = " from XWikiDocument as doc, BaseObject as obj"; + String where = " where doc.fullName=obj.name and obj.className='" + classname + "'"; + if (idField.startsWith("doc.")||idField.startsWith("obj.")) + select += idField + ","; + else { + select += "idprop.value,"; + tables += ", StringProperty as idprop"; + where += " and obj.id=idprop.id.id and idprop.id.name='" + idField + "'"; + } + if (valueField.startsWith("doc.")||valueField.startsWith("obj.")) + select += valueField + ","; + else { + if (idField.equals(valueField)) { select += "idprop.value,"; - tables += ", StringProperty as idprop"; - where += " and obj.id=idprop.id.id and idprop.id.name='" + idField + "'"; + } else { + select += "valueprop.value,"; + tables += ", StringProperty as valueprop"; + where += " and obj.id=valueprop.id.id and valueprop.id.name='" + valueField + "'"; } - if (valueField.startsWith("doc.")||valueField.startsWith("obj.")) - select += valueField + ","; - else { - if (idField.equals(valueField)) { - select += "idprop.value,"; - } else { - select += "valueprop.value,"; - tables += ", StringProperty as valueprop"; - where += " and obj.id=valueprop.id.id and valueprop.id.name='" + valueField + "'"; - } - } + } - if (parentField.startsWith("doc.")||parentField.startsWith("obj.")) - select += parentField + ","; - else { - if (idField.equals(parentField)) { - select += "idprop.value,"; - } else if (valueField.equals(parentField)) { - select += "valueprop.value,"; - } else { - select += "parentprop.value,"; - tables += ", StringProperty as parentprop"; - where += " and obj.id=parentprop.id.id and parentprop.id.name='" + parentField + "'"; - } + if (parentField.startsWith("doc.")||parentField.startsWith("obj.")) + select += parentField + ","; + else { + if (idField.equals(parentField)) { + select += "idprop.value,"; + } else if (valueField.equals(parentField)) { + select += "valueprop.value,"; + } else { + select += "parentprop.value,"; + tables += ", StringProperty as parentprop"; + where += " and obj.id=parentprop.id.id and parentprop.id.name='" + parentField + "'"; } - // Let's create the sql - sql = select + tables + where; - } else { - // TODO: query plugin impl. - // We need to generate the right query for the query plugin } - + // Let's create the sql + sql = select + tables + where; } - return context.getWiki().parseContent(sql, context); + return context.getWiki().parseContent(sql, context); } } Index: XWikiException.java =================================================================== --- XWikiException.java (revision 3012) +++ XWikiException.java (working copy) @@ -411,4 +411,4 @@ pwriter.flush(); return swriter.getBuffer().toString(); } -} \ No newline at end of file +} Index: XWikiInterface.java =================================================================== --- XWikiInterface.java (revision 3012) +++ XWikiInterface.java (working copy) @@ -171,8 +171,6 @@ void setConfig(XWikiConfig config); - void setStore(XWikiStoreInterface store); - void setVersion(String version); XWikiNotificationManager getNotificationManager(); Index: store/XWikiStoreManager.java =================================================================== --- store/XWikiStoreManager.java (revision 0) +++ store/XWikiStoreManager.java (revision 0) @@ -0,0 +1,149 @@ +/* + * Copyright 2007, XpertNet SARL, and individual contributors as indicated + * by the contributors.txt. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + + +package com.xpn.xwiki.store; + +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.XWikiException; + +import java.lang.reflect.InvocationTargetException; + +/** + * XWikiStoreManager: + * Set up the storage module for XWiki. Among other things, it + * instantiates the chosen store classes and store cache class. + * + */ + +public class XWikiStoreManager { + + // The upper store in the storage hierarchy, for now + // it is either the baseStore or the cacheStore. + private XWikiStoreInterface store; + + private XWikiStoreInterface baseStore; + private XWikiCacheStoreInterface cacheStore; + private XWikiAttachmentStoreInterface attachmentStore; + private XWikiVersioningStoreInterface versioningStore; + + /** + * Instantiates an XWikiStore tailored to the configuration + * parameters in xwiki.cfg. + * + * @throws XWikiException + * @param context the XWiki context + */ + public XWikiStoreManager(XWikiContext context) throws XWikiException + { + // Retrieve the pertinent configuration parameters + + String storeClass = context.getWiki().Param( + "xwiki.store.class", + "com.xpn.xwiki.store.XWikiHibernateStore"); + String attachmentClass = context.getWiki().Param( + "xwiki.attachment.class", + "com.xpn.xwiki.store.XWikiHibernateAttachmentStore"); + String versioningClass = context.getWiki().Param( + "xwiki.store.versioning.class", + "com.xpn.xwiki.store.XWikiHibernateVersioningStore"); + + boolean useCache = "0".equals + (context.getWiki().Param("xwiki.store.cache", "1")); + + // Initialize the different stores + + baseStore = (XWikiStoreInterface) createStore(storeClass, context); + + if (useCache) { + cacheStore = new XWikiCacheStore(baseStore, context); + store = cacheStore; + } else + store = baseStore; + + attachmentStore = + (XWikiAttachmentStoreInterface) createStore(attachmentClass, context); + + versioningStore = + (XWikiVersioningStoreInterface) createStore(versioningClass, context); + } + + /** + * Flushes any cache instantiated by this store manager. + */ + public void flushCache() + { + if (null != cacheStore) + cacheStore.flushCache(); + } + + /** + * Creates the store corresponding to the + * given class. + * + * @throws XWikiException + * @param className the name of the class to be instantiated + * @param context the XWiki context + * @return the new store + */ + private Object createStore(String className, XWikiContext context) + throws XWikiException + { + try { + // Try to instantiate the class provided by the user + + return Class.forName(className) + .getConstructor(new Class[] {XWikiContext.class}) + .newInstance(new Object[] {context}); + } + catch (Throwable e) + { + e = (e.getCause() == null) ? e : e.getCause(); + + throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, + XWikiException.ERROR_XWIKI_STORE_CLASSINVOCATIONERROR, + "Cannot load store class {0}", + e, + new Object [] {className}); + } + } + + // Various getters to the different stores + + public XWikiStoreInterface getNotCacheStore() + { + return baseStore; + } + + public XWikiStoreInterface getStore() + { + return store; + } + + public XWikiAttachmentStoreInterface getAttachmentStore() + { + return attachmentStore; + } + + public XWikiVersioningStoreInterface getVersioningStore() + { + return versioningStore; + } +} Index: store/XWikiHibernateAttachmentStore.java =================================================================== --- store/XWikiHibernateAttachmentStore.java (revision 3012) +++ store/XWikiHibernateAttachmentStore.java (working copy) @@ -29,11 +29,10 @@ * THis allows to initialize our storage engine. * The hibernate config file path is taken from xwiki.cfg * or directly in the WEB-INF directory. - * @param xwiki * @param context */ - public XWikiHibernateAttachmentStore(XWiki xwiki, XWikiContext context) { - super(xwiki, context); + public XWikiHibernateAttachmentStore(XWikiContext context) { + super(context); } /** @@ -299,4 +298,4 @@ } catch (Exception e) {} } } -} \ No newline at end of file +} Index: store/XWikiHibernateBaseStore.java =================================================================== --- store/XWikiHibernateBaseStore.java (revision 3012) +++ store/XWikiHibernateBaseStore.java (working copy) @@ -50,15 +50,27 @@ private String hibpath; private URL hiburl; + /** + * @deprecated We should make no assumption on the underlying sql dialect. + * This method is only here for backwards compatibility. + * + * @return True if the dialect used is mySQL. + */ + public boolean isMySQL() + { + return "net.sf.hibernate.dialect.MySQLDialect".equals( + getConfiguration().getProperties().get("dialect")); + } + /** - * THis allows to initialize our storage engine. + * This allows to initialize our storage engine. * The hibernate config file path is taken from xwiki.cfg * or directly in the WEB-INF directory. - * @param xwiki * @param context */ - public XWikiHibernateBaseStore(XWiki xwiki, XWikiContext context) { - String path = xwiki.Param("xwiki.store.hibernate.path", "hibernate.cfg.xml"); + public XWikiHibernateBaseStore(XWikiContext context) { + String path = context.getWiki().Param( + "xwiki.store.hibernate.path", "hibernate.cfg.xml"); if ((path!=null)&&((new File(path).exists() || context.getEngineContext() == null))) { setPath (path); } else { @@ -231,7 +243,7 @@ } try { - String fullName = ((context!=null)&&(context.getWiki()!=null)&&(context.getWiki().isMySQL())) ? "concat('xwd_web','.','xwd_name)" : "xwd_fullname"; + String fullName = (isMySQL()) ? "concat('xwd_web','.','xwd_name)" : "xwd_fullname"; String[] schemaSQL = getSchemaUpdateScript(getConfiguration(), context); String[] addSQL = { // Make sure we have no null valued in integer fields Index: store/XWikiStoreInterface.java =================================================================== --- store/XWikiStoreInterface.java (revision 3012) +++ store/XWikiStoreInterface.java (working copy) @@ -69,4 +69,35 @@ public List searchDocuments(String wheresql, boolean distinctbyname, boolean customMapping, boolean checkRight, int nb, int start, XWikiContext context) throws XWikiException; public void injectUpdatedCustomMappings(XWikiContext context) throws XWikiException; public List getTranslationList(XWikiDocument doc, XWikiContext context) throws XWikiException; + + /** + * Updates the store schema. + * + * @param context the XWiki context + * @param force forces the update, even if the configuration + * parameters say otherwise + */ + public void updateSchema(XWikiContext context, boolean force); + + /** + * Return the XWiki Spaces this store knows about. + * + * @param context the XWiki context + * @return the list of spaces names + * @throws XWikiException + */ + public List getSpaces(XWikiContext context) throws XWikiException; + + /** + * Return the names of all the documents in + * given XWiki space. + * + * @param spaceNames + * @param context the XWiki context + * @return the list of spaces names + * @throws XWikiException + */ + public List getSpaceDocsNames(String spaceName, XWikiContext context) + throws XWikiException; + } Index: store/XWikiHibernateStore.java =================================================================== --- store/XWikiHibernateStore.java (revision 3012) +++ store/XWikiHibernateStore.java (working copy) @@ -66,15 +66,41 @@ private static final Log log = LogFactory.getLog(XWikiHibernateStore.class); private Map validTypesMap = new HashMap(); + /** + * @deprecated Outside package com.xpn.xwiki.store we should make no assumptions on the + * storage type. This method is here to support code still needing a raw access + * to the hibernate store. + * + * Returns, if it exists, the XWikiHibernateStore that is being used. + * @param context the XWikiContext + * @return the XWiki hibernate store + * @throws XWikiException + */ + public static XWikiHibernateStore getHibernateStore(XWikiContext context) + throws XWikiException + { + XWikiStoreInterface realStore = + context.getWiki().getNotCacheStore(); + + if (realStore instanceof XWikiHibernateStore) + return (XWikiHibernateStore) realStore; + else + throw new XWikiException( + XWikiException.MODULE_XWIKI_STORE, + XWikiException.ERROR_XWIKI_DOES_NOT_EXIST, + "XWiki is not using an hibernate store.", + null, null); + } + /** * This allows to initialize our storage engine. * The hibernate config file path is taken from xwiki.cfg * or directly in the WEB-INF directory. - * @param xwiki + * * @param context */ - public XWikiHibernateStore(XWiki xwiki, XWikiContext context) { - super(xwiki, context); + public XWikiHibernateStore(XWikiContext context) { + super(context); initValidColumTypes(); } @@ -2134,7 +2160,22 @@ } return result; } + + public List getSpaces(XWikiContext context) throws XWikiException + { + return context.getWiki() + .search("select distinct doc.web from XWikiDocument doc", context); + } + public List getSpaceDocsNames(String spaceName, XWikiContext context) + throws XWikiException + { + return context.getWiki() + .search("select distinct doc.name from XWikiDocument doc", + new Object[][] {{"doc.web", spaceName}}, + context); + } + /* public void updateArchive(String text) throws XWikiException { try { Index: store/XWikiCacheStore.java =================================================================== --- store/XWikiCacheStore.java (revision 3012) +++ store/XWikiCacheStore.java (working copy) @@ -345,4 +345,19 @@ public List getTranslationList(XWikiDocument doc, XWikiContext context) throws XWikiException { return store.getTranslationList(doc, context); } + + public void updateSchema(XWikiContext context, boolean force) { + store.updateSchema(context, force); + } + + public List getSpaces(XWikiContext context) throws XWikiException + { + return store.getSpaces(context); + } + + public List getSpaceDocsNames(String spaceName, XWikiContext context) + throws XWikiException + { + return store.getSpaceDocsNames(spaceName, context); + } } Index: store/jcr/XWikiJcrPropertyVersioningStore.java =================================================================== --- store/jcr/XWikiJcrPropertyVersioningStore.java (revision 3012) +++ store/jcr/XWikiJcrPropertyVersioningStore.java (working copy) @@ -18,8 +18,8 @@ /** Versions store in jcr property '@archive' of xwiki:document */ public class XWikiJcrPropertyVersioningStore extends XWikiJcrBaseStore implements XWikiVersioningStoreInterface { - public XWikiJcrPropertyVersioningStore(XWiki xwiki, XWikiContext context) throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { - super(xwiki, context); + public XWikiJcrPropertyVersioningStore(XWikiContext context) throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { + super(context); } public void saveXWikiDocArchive(final XWikiDocumentArchive archivedoc, boolean bTransaction, XWikiContext context) throws XWikiException { Index: store/jcr/XWikiJcrAttachmentStore.java =================================================================== --- store/jcr/XWikiJcrAttachmentStore.java (revision 3012) +++ store/jcr/XWikiJcrAttachmentStore.java (working copy) @@ -17,8 +17,8 @@ import java.util.List; public class XWikiJcrAttachmentStore extends XWikiJcrBaseStore implements XWikiAttachmentStoreInterface { - public XWikiJcrAttachmentStore(XWiki xwiki, XWikiContext context) throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { - super(xwiki, context); + public XWikiJcrAttachmentStore(XWikiContext context) throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { + super(context); } public void saveAttachmentContent(XWikiAttachment attachment, XWikiContext context, boolean bTransaction) throws XWikiException { Index: store/jcr/XWikiJcrBaseStore.java =================================================================== --- store/jcr/XWikiJcrBaseStore.java (revision 3012) +++ store/jcr/XWikiJcrBaseStore.java (working copy) @@ -42,13 +42,13 @@ public class XWikiJcrBaseStore { IJcrProvider jcr; - public XWikiJcrBaseStore(XWiki xwiki, XWikiContext context) throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { + public XWikiJcrBaseStore(XWikiContext context) throws SecurityException, NoSuchMethodException, ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { super(); - if (xwiki.getStore() instanceof XWikiJcrBaseStore) { - XWikiJcrBaseStore jcrstore = (XWikiJcrBaseStore) xwiki.getStore(); + if (context.getWiki().getStore() instanceof XWikiJcrBaseStore) { + XWikiJcrBaseStore jcrstore = (XWikiJcrBaseStore) context.getWiki().getStore(); this.jcr = jcrstore.getJcrProvider(); } else { - String sprovider = xwiki.Param("xwiki.store.jcr.provider"); + String sprovider = context.getWiki().Param("xwiki.store.jcr.provider"); Constructor cn = Class.forName(sprovider).getConstructor(new Class[]{XWikiConfig.class, XWikiContext.class}); IJcrProvider jcr = (IJcrProvider) cn.newInstance(new Object[]{context.getWiki().getConfig(), context}); this.jcr = jcr; Index: store/jcr/XWikiJcrStore.java =================================================================== --- store/jcr/XWikiJcrStore.java (revision 3012) +++ store/jcr/XWikiJcrStore.java (working copy) @@ -81,8 +81,8 @@ /** XWikiJCRStore - XWiki Store System backend to JCR */ public class XWikiJcrStore extends XWikiJcrBaseStore implements XWikiStoreInterface { - public XWikiJcrStore(XWiki xwiki, XWikiContext context) throws SecurityException, IllegalArgumentException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, XWikiException { - super(xwiki, context); + public XWikiJcrStore(XWikiContext context) throws SecurityException, IllegalArgumentException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, XWikiException { + super(context); // create default wiki createWiki("xwiki", context); } @@ -758,7 +758,7 @@ } } - public List getSpaceDocsName(final String spaceName, XWikiContext context) throws XWikiException { + public List getSpaceDocsNames(final String spaceName, XWikiContext context) throws XWikiException { try { return (List) executeRead(context, new JcrCallBack(){ public Object doInJcr(XWikiJcrSession session) throws Exception { @@ -923,4 +923,8 @@ notSupportedCall(); return null; } + + public void updateSchema(XWikiContext context, boolean force) { + // No schema to update in JCR. + } } Index: store/XWikiHibernateVersioningStore.java =================================================================== --- store/XWikiHibernateVersioningStore.java (revision 3012) +++ store/XWikiHibernateVersioningStore.java (working copy) @@ -30,11 +30,10 @@ * THis allows to initialize our storage engine. * The hibernate config file path is taken from xwiki.cfg * or directly in the WEB-INF directory. - * @param xwiki * @param context */ - public XWikiHibernateVersioningStore(XWiki xwiki, XWikiContext context) { - super(xwiki, context); + public XWikiHibernateVersioningStore(XWikiContext context) { + super(context); } /** Index: plugin/query/QueryPlugin.java =================================================================== --- plugin/query/QueryPlugin.java (revision 3012) +++ plugin/query/QueryPlugin.java (working copy) @@ -42,6 +42,7 @@ import com.xpn.xwiki.objects.classes.BaseClass; import com.xpn.xwiki.plugin.XWikiDefaultPlugin; import com.xpn.xwiki.plugin.XWikiPluginInterface; +import com.xpn.xwiki.store.XWikiHibernateStore; import com.xpn.xwiki.store.XWikiStoreInterface; import com.xpn.xwiki.store.jcr.XWikiJcrStore; @@ -79,7 +80,7 @@ } boolean isHibernate() { - return getContext().getWiki().getHibernateStore() != null; + return getContext().getWiki().getNotCacheStore() instanceof XWikiHibernateStore; } boolean isJcr() { return getContext().getWiki().getNotCacheStore() instanceof XWikiJcrStore; Index: plugin/query/HibernateQuery.java =================================================================== --- plugin/query/HibernateQuery.java (revision 3012) +++ plugin/query/HibernateQuery.java (working copy) @@ -62,6 +62,7 @@ import com.xpn.xwiki.objects.classes.PropertyClass; import com.xpn.xwiki.plugin.query.HibernateQuery.XWikiHibernateQueryTranslator.ObjProperty; import com.xpn.xwiki.store.XWikiHibernateStore; +import com.xpn.xwiki.store.XWikiStoreInterface; import com.xpn.xwiki.util.Util; /** Query implementation for Hibernate */ public class HibernateQuery extends DefaultQuery { @@ -71,9 +72,9 @@ public HibernateQuery(QueryRootNode tree, IQueryFactory qf) { super(tree, qf); } - public XWikiHibernateStore getHibernateStore() { - return getContext().getWiki().getHibernateStore(); - } + public XWikiHibernateStore getHibernateStore() throws XWikiException { + return XWikiHibernateStore.getHibernateStore(getContext()); + } /** @return true, if something added */ protected boolean constructWhere(StringBuffer sb) { Index: stats/impl/XWikiStatsServiceImpl.java =================================================================== --- stats/impl/XWikiStatsServiceImpl.java (revision 3012) +++ stats/impl/XWikiStatsServiceImpl.java (working copy) @@ -98,9 +98,9 @@ * @return */ public DocumentStats getDocMonthStats(String docname, String action, Date month, XWikiContext context) { - XWikiHibernateStore store = context.getWiki().getHibernateStore(); DocumentStats object = new DocumentStats(docname, action, month, XWikiStats.PERIOD_MONTH); try { + XWikiHibernateStore store = XWikiHibernateStore.getHibernateStore(context); store.loadXWikiCollection(object, context, true); return object; } catch (XWikiException e) { @@ -191,8 +191,13 @@ if (("".equals(statsactive))&&("0".equals(statsdefault))) return; - XWikiHibernateStore store = context.getWiki().getHibernateStore(); - if (store == null) return; + XWikiHibernateStore store; + try { + store = XWikiHibernateStore.getHibernateStore(context); + } + catch (Exception e) { + return; + } VisitStats vobject = findVisit(context); // We count page views in the sessions only for the "view" action @@ -443,7 +448,7 @@ return null; } } - XWikiHibernateStore store = context.getWiki().getHibernateStore(); + XWikiHibernateStore store = XWikiHibernateStore.getHibernateStore(context); try { store.beginTransaction(context); Session session = store.getSession(context); @@ -488,7 +493,7 @@ return null; } } - XWikiHibernateStore store = context.getWiki().getHibernateStore(); + XWikiHibernateStore store = XWikiHibernateStore.getHibernateStore(context); try { store.beginTransaction(context); Session session = store.getSession(context); Index: api/XWiki.java =================================================================== --- api/XWiki.java (revision 3012) +++ api/XWiki.java (working copy) @@ -1733,7 +1733,7 @@ */ public String getFullNameSQL() { - return xwiki.getFullNameSQL(); + return "doc.fullName"; } /** Index: XWiki.java =================================================================== --- XWiki.java (revision 3012) +++ XWiki.java (working copy) @@ -71,9 +71,7 @@ import org.apache.velocity.VelocityContext; import org.exoplatform.container.PortalContainer; import org.exoplatform.container.RootContainer; -import org.hibernate.HibernateException; import org.securityfilter.filter.URLPatternMatcher; - import com.xpn.xwiki.api.Api; import com.xpn.xwiki.api.Document; import com.xpn.xwiki.api.User; @@ -116,12 +114,9 @@ import com.xpn.xwiki.stats.impl.SearchEngineRule; import com.xpn.xwiki.stats.impl.XWikiStatsServiceImpl; import com.xpn.xwiki.store.XWikiAttachmentStoreInterface; -import com.xpn.xwiki.store.XWikiCacheStore; -import com.xpn.xwiki.store.XWikiCacheStoreInterface; -import com.xpn.xwiki.store.XWikiHibernateStore; import com.xpn.xwiki.store.XWikiStoreInterface; +import com.xpn.xwiki.store.XWikiStoreManager; import com.xpn.xwiki.store.XWikiVersioningStoreInterface; -import com.xpn.xwiki.store.jcr.XWikiJcrStore; import com.xpn.xwiki.user.api.XWikiAuthService; import com.xpn.xwiki.user.api.XWikiGroupService; import com.xpn.xwiki.user.api.XWikiRightService; @@ -150,12 +145,8 @@ private XWikiConfig config; - private XWikiStoreInterface store; + private XWikiStoreManager storeManager; - private XWikiAttachmentStoreInterface attachmentStore; - - private XWikiVersioningStoreInterface versioningStore; - private XWikiRenderingEngine renderingEngine; private XWikiPluginManager pluginManager; @@ -295,43 +286,19 @@ } } - public XWikiStoreInterface getNotCacheStore() + public void updateDatabase(String appname, XWikiContext context) throws XWikiException { - XWikiStoreInterface store = getStore(); - if (store instanceof XWikiCacheStoreInterface) - store = ((XWikiCacheStoreInterface) store).getStore(); - return store; - } - - public XWikiHibernateStore getHibernateStore() - { - XWikiStoreInterface store = getStore(); - if (store instanceof XWikiHibernateStore) - return (XWikiHibernateStore) store; - else if (store instanceof XWikiCacheStoreInterface) { - store = ((XWikiCacheStoreInterface) store).getStore(); - if (store instanceof XWikiHibernateStore) - return (XWikiHibernateStore) store; - else - return null; - } else - return null; - } - - public void updateDatabase(String appname, XWikiContext context) throws HibernateException, - XWikiException - { updateDatabase(appname, false, context); } public void updateDatabase(String appname, boolean force, XWikiContext context) - throws HibernateException, XWikiException + throws XWikiException { updateDatabase(appname, force, true, context); } public void updateDatabase(String appname, boolean force, boolean initClasses, - XWikiContext context) throws HibernateException, XWikiException + XWikiContext context) throws XWikiException { synchronized (appname) { String database = context.getDatabase(); @@ -341,9 +308,7 @@ context.setDatabase(appname); if (!wikilist.contains(appname)) { wikilist.add(appname); - XWikiHibernateStore store = getHibernateStore(); - if (store != null) - store.updateSchema(context, force); + getStore().updateSchema(context, force); } // Make sure these classes exists @@ -444,7 +409,7 @@ try { // Let's make sure the virtaul wikis are upgraded to the latest database version xwiki.updateDatabase(appname, false, context); - } catch (HibernateException e) { + } catch (XWikiException e) { // Just to report it e.printStackTrace(); } @@ -576,88 +541,10 @@ // Create the notification manager setNotificationManager(new XWikiNotificationManager()); - // Prepare the store - XWikiStoreInterface basestore; setConfig(config); - String storeclass = Param("xwiki.store.class", "com.xpn.xwiki.store.XWikiHibernateStore"); - try { - Class[] classes = new Class[] {XWiki.class, XWikiContext.class}; - Object[] args = new Object[] {this, context}; - basestore = - (XWikiStoreInterface) Class.forName(storeclass).getConstructor(classes) - .newInstance(args); - } catch (InvocationTargetException e) { - Object[] args = {storeclass}; - throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, - XWikiException.ERROR_XWIKI_STORE_CLASSINVOCATIONERROR, - "Cannot load store class {0}", - e.getTargetException(), - args); - } catch (Exception e) { - Object[] args = {storeclass}; - throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, - XWikiException.ERROR_XWIKI_STORE_CLASSINVOCATIONERROR, - "Cannot load store class {0}", - e, - args); - } + + storeManager = new XWikiStoreManager(context); - // Check if we need to use the cache store.. - boolean nocache = "0".equals(Param("xwiki.store.cache", "1")); - if (!nocache) { - XWikiCacheStoreInterface cachestore = new XWikiCacheStore(basestore, context); - setStore(cachestore); - } else - setStore(basestore); - - String attachmentStoreclass = - Param("xwiki.store.attachment.class", - "com.xpn.xwiki.store.XWikiHibernateAttachmentStore"); - try { - Class[] classes = new Class[] {XWiki.class, XWikiContext.class}; - Object[] args = new Object[] {this, context}; - setAttachmentStore((XWikiAttachmentStoreInterface) Class - .forName(attachmentStoreclass).getConstructor(classes).newInstance(args)); - } catch (InvocationTargetException e) { - Object[] args = {attachmentStoreclass}; - throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, - XWikiException.ERROR_XWIKI_STORE_CLASSINVOCATIONERROR, - "Cannot load attachment store class {0}", - e.getTargetException(), - args); - } catch (Exception e) { - Object[] args = {attachmentStoreclass}; - throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, - XWikiException.ERROR_XWIKI_STORE_CLASSINVOCATIONERROR, - "Cannot load attachment store class {0}", - e, - args); - } - - String versioningStoreclass = - Param("xwiki.store.versioning.class", - "com.xpn.xwiki.store.XWikiHibernateVersioningStore"); - try { - Class[] classes = new Class[] {XWiki.class, XWikiContext.class}; - Object[] args = new Object[] {this, context}; - setVersioningStore((XWikiVersioningStoreInterface) Class - .forName(versioningStoreclass).getConstructor(classes).newInstance(args)); - } catch (InvocationTargetException e) { - Object[] args = {versioningStoreclass}; - throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, - XWikiException.ERROR_XWIKI_STORE_CLASSINVOCATIONERROR, - "Cannot load versioning store class {0}", - e.getTargetException(), - args); - } catch (Exception e) { - Object[] args = {versioningStoreclass}; - throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, - XWikiException.ERROR_XWIKI_STORE_CLASSINVOCATIONERROR, - "Cannot load versioning store class {0}", - e, - args); - } - resetRenderingEngine(context); // Prepare the Plugin Engine @@ -867,19 +754,24 @@ } } + public XWikiStoreInterface getNotCacheStore() + { + return storeManager.getNotCacheStore(); + } + public XWikiStoreInterface getStore() { - return store; + return storeManager.getStore(); } public XWikiAttachmentStoreInterface getAttachmentStore() { - return attachmentStore; + return storeManager.getAttachmentStore(); } public XWikiVersioningStoreInterface getVersioningStore() { - return versioningStore; + return storeManager.getVersioningStore(); } public void saveDocument(XWikiDocument doc, XWikiContext context) throws XWikiException @@ -1983,11 +1875,9 @@ if (groupService != null) groupService.flushCache(); - // If we use the Cache Store layer.. we need to flush it - XWikiStoreInterface store = getStore(); - if ((store != null) && (store instanceof XWikiCacheStoreInterface)) { - ((XWikiCacheStoreInterface) getStore()).flushCache(); - } + // Flush the potential cache in the Store Manager + storeManager.flushCache(); + // Flush renderers.. Groovy renderer has a cache XWikiRenderingEngine rengine = getRenderingEngine(); if (rengine != null) @@ -2013,22 +1903,7 @@ { this.config = config; } - - public void setStore(XWikiStoreInterface store) - { - this.store = store; - } - - public void setAttachmentStore(XWikiAttachmentStoreInterface attachmentStore) - { - this.attachmentStore = attachmentStore; - } - - public void setVersioningStore(XWikiVersioningStoreInterface versioningStore) - { - this.versioningStore = versioningStore; - } - + public void setVersion(String version) { this.version = version; @@ -2799,6 +2674,7 @@ throws XWikiException { BaseClass gclass = getGroupClass(context); + gclass.setUserDefaultGroup(fullwikiname, context); XWikiDocument allgroupdoc = getDocument("XWiki.XWikiAllGroup", context); @@ -2807,11 +2683,21 @@ memberobj.setName(allgroupdoc.getFullName()); memberobj.setStringValue("member", fullwikiname); allgroupdoc.addObject(gclass.getName(), memberobj); + saveDocument(allgroupdoc, null, context); + + /* TODO: saveXWikiObject is Hibernate specific, but the above method + * should work for all the stores. + * + * Should check if + * getDocument + addObject + saveDocument <=> + * saveXWikiObject + * ? + * if (allgroupdoc.isNew()) { saveDocument(allgroupdoc, null, context); } else { getHibernateStore().saveXWikiObject(memberobj, context, true); - } + }*/ try { XWikiGroupService gservice = getGroupService(context); @@ -4123,31 +4009,6 @@ return result; } - public boolean isMySQL() - { - return "net.sf.hibernate.dialect.MySQLDialect".equals(getHibernateStore() - .getConfiguration().getProperties().get("dialect")); - } - - public String getFullNameSQL() - { - return getFullNameSQL(true); - } - - public String getFullNameSQL(boolean newFullName) - { - if (newFullName) - return "doc.fullName"; - - if (fullNameSQL == null) { - if (isMySQL()) - fullNameSQL = "CONCAT(doc.web,'.',doc.name)"; - else - fullNameSQL = "doc.web||'.'||doc.name"; - } - return fullNameSQL; - } - public String getDocName(String docname) { return docname.substring(docname.indexOf(".") + 1); @@ -4504,26 +4365,12 @@ public List getSpaces(XWikiContext context) throws XWikiException { - List webs = null; - if (getNotCacheStore() instanceof XWikiHibernateStore) { - webs = this.search("select distinct doc.web from XWikiDocument doc", context); - } else if (getNotCacheStore() instanceof XWikiJcrStore) { - webs = ((XWikiJcrStore) getNotCacheStore()).getSpaces(context); - } - return webs; + return getNotCacheStore().getSpaces(context); } public List getSpaceDocsName(String spaceName, XWikiContext context) throws XWikiException { - List docs = null; - if (getNotCacheStore() instanceof XWikiHibernateStore) { - docs = - this.search("select distinct doc.name from XWikiDocument doc", new Object[][] {{ - "doc.web", spaceName}}, context); - } else if (getNotCacheStore() instanceof XWikiJcrStore) { - docs = ((XWikiJcrStore) getNotCacheStore()).getSpaceDocsName(spaceName, context); - } - return docs; + return getNotCacheStore().getSpaceDocsNames(spaceName, context); } public List getIncludedMacros(String defaultweb, String content, XWikiContext context) @@ -4599,19 +4446,18 @@ public void refreshLinks(XWikiContext context) throws XWikiException { - // refreshes all Links of each doc of the wiki - List docs = null; - if (getNotCacheStore() instanceof XWikiHibernateStore) { - docs = this.search("select doc.fullName from XWikiDocument as doc", context); - } else if (getNotCacheStore() instanceof XWikiJcrStore) { - docs = ((XWikiJcrStore) getNotCacheStore()).getAllDocuments(context); - } else { - return; + Iterator spaces = getSpaces(context).iterator(); + List docs; + + while (spaces.hasNext()) { + String space = (String) spaces.next(); + Iterator names = getSpaceDocsName(space, context).iterator(); + while(names.hasNext()) { + XWikiDocument doc = + getDocument(space, (String) names.next(), context); + doc.getStore().saveLinks(doc, context, true); + } } - for (int i = 0; i < docs.size(); i++) { - XWikiDocument myDoc = this.getDocument((String) docs.get(i), context); - myDoc.getStore().saveLinks(myDoc, context, true); - } } public boolean hasBacklinks(XWikiContext context) Index: web/PropUpdateAction.java =================================================================== --- web/PropUpdateAction.java (revision 3012) +++ web/PropUpdateAction.java (working copy) @@ -80,8 +80,9 @@ // We need to load all documents that use this property and rename it if (fieldsToRename.size() > 0) { List list = xwiki.getStore().searchDocumentsNames( - ", BaseObject as obj where obj.name=" + xwiki.getFullNameSQL() + " and obj.className='" + Utils.SQLFilter(bclass.getName()) + "' and " - + xwiki.getFullNameSQL() + "<> '" + Utils.SQLFilter(bclass.getName()) + "'", context); + ", BaseObject as obj where obj.name=doc.fullName" + + " and obj.className='" + Utils.SQLFilter(bclass.getName()) + "' and " + + "doc.fullName <> '" + Utils.SQLFilter(bclass.getName()) + "'", context); for (int i = 0; i < list.size(); i++) { XWikiDocument doc2 = xwiki.getDocument((String) list.get(i), context); doc2.renameProperties(bclass.getName(), fieldsToRename); Index: user/impl/xwiki/XWikiGroupServiceImpl.java =================================================================== --- user/impl/xwiki/XWikiGroupServiceImpl.java (revision 3012) +++ user/impl/xwiki/XWikiGroupServiceImpl.java (working copy) @@ -91,10 +91,10 @@ list = (List) groupCache.getFromCache(key); } catch (XWikiCacheNeedsRefreshException e) { groupCache.cancelUpdate(key); - + if (context.getWiki().getNotCacheStore() instanceof XWikiHibernateStore) { list = context.getWiki().getStore().searchDocumentsNames(", BaseObject as obj, StringProperty as prop " - + "where obj.name=" + context.getWiki().getFullNameSQL() + " and obj.className='XWiki.XWikiGroups' " + + "where obj.name=doc.fullName and obj.className='XWiki.XWikiGroups' " + "and obj.id = prop.id.id and prop.id.name='member' " + "and (prop.value='" + Utils.SQLFilter(username) + "' or prop.value='" + Utils.SQLFilter(shortname) + "' or prop.value='" @@ -139,7 +139,7 @@ try { if (group == null) { - if (context.getWiki().getHibernateStore() != null) { + if (context.getWiki().getNotCacheStore() instanceof XWikiHibernateStore) { sql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='XWiki.XWikiUsers'"; return context.getWiki().getStore().searchDocumentsNames(sql, context); } else if (context.getWiki().getNotCacheStore() instanceof XWikiJcrStore) { @@ -176,7 +176,7 @@ } public List listAllGroups(XWikiContext context) throws XWikiException { - if (context.getWiki().getHibernateStore()!=null) { + if (context.getWiki().getNotCacheStore() instanceof XWikiHibernateStore) { String sql = ", BaseObject as obj where obj.name=doc.fullName and obj.className='XWiki.XWikiGroups'"; return context.getWiki().getStore().searchDocumentsNames(sql, context); } else if (context.getWiki().getNotCacheStore() instanceof XWikiJcrStore) {