📄 xwikihibernatestore.java
字号:
public void endTransaction(XWikiContext context, boolean commit) throws HibernateException { Session session = null; try { session = getSession(context); Transaction transaction = getTransaction(context); setSession(null, context); setTransaction(null, context); if (transaction!=null) { if ( log.isDebugEnabled() ) log.debug("Releasing hibernate transaction " + transaction); if (commit) { transaction.commit(); } else { // Don't commit the transaction, can be faster for read-only operations transaction.rollback(); } } } finally { if (session!=null) closeSession(session); } } private void closeSession(Session session) throws HibernateException { if (session!=null) { try { if ( log.isDebugEnabled() ) log.debug("Releasing hibernate session " + session); Connection connection = session.connection(); if ((connection!=null)) { nbConnections--; try { removeConnection(connection); } catch (Throwable e) { // This should not happen e.printStackTrace(); } } } finally { session.close(); } } } public void cleanUp(XWikiContext context) { try { Session session = getSession(context); if (session!=null) { if ( log.isWarnEnabled() ) log.warn("Cleanup of session was needed: " + session); endTransaction(context, false); } } catch (HibernateException e) { } } public void createWiki(String wikiName, XWikiContext context) throws XWikiException { boolean bTransaction = true; String database = context.getDatabase(); Statement stmt = null; try { bTransaction = beginTransaction(context); Session session = getSession(context); Connection connection = session.connection(); stmt = connection.createStatement(); stmt.execute("create database " + wikiName); endTransaction(context, true); } catch (Exception e) { Object[] args = { wikiName }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_CREATE_DATABASE, "Exception while create wiki database {0}", e, args); } finally { context.setDatabase(database); try { if (stmt!=null) stmt.close(); } catch (Exception e) {} try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} } }public boolean exists(XWikiDocument doc, XWikiContext context) throws XWikiException { boolean bTransaction = true; MonitorPlugin monitor = getMonitorPlugin(context); try { doc.setStore(this); checkHibernate(context); // Start monitoring timer if (monitor!=null) monitor.startTimer("hibernate"); bTransaction = bTransaction && beginTransaction(context); Session session = getSession(context); String fullName = doc.getFullName(); String sql = "select doc.fullName from XWikiDocument as doc where doc.fullName=:fullName"; if (monitor!=null) monitor.setTimerDesc("hibernate", sql); Query query = session.createQuery(sql); query.setString("fullName", fullName); Iterator it = query.list().iterator(); while (it.hasNext()) { if (fullName.equals(it.next())) return true; } return false; } catch (Exception e) { Object[] args = { doc.getFullName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_CHECK_EXISTS_DOC, "Exception while reading document {0}", e, args); } finally { // End monitoring timer if (monitor!=null) monitor.endTimer("hibernate"); try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} } } public void saveXWikiDoc(XWikiDocument doc, XWikiContext context, boolean bTransaction) throws XWikiException { MonitorPlugin monitor = getMonitorPlugin(context); try { // Start monitoring timer if (monitor!=null) monitor.startTimer("hibernate"); doc.setStore(this); if (bTransaction) { checkHibernate(context); bTransaction = beginTransaction(context); } Session session = getSession(context); saveAttachmentList(doc, context, false); // Handle the latest text file if (doc.isContentDirty()||doc.isMetaDataDirty()) { doc.setDate(new Date()); doc.incrementVersion(); doc.updateArchive(doc.toXML(context)); } else { // Make sure the getArchive call has been made once // with a valid context try { doc.getArchive(context); } catch (XWikiException e) { // this is a non critical error } } // Verify if the document already exists Query query = session.createQuery("select xwikidoc.id from XWikiDocument as xwikidoc where xwikidoc.id = :id"); query.setLong("id", doc.getId()); if (query.uniqueResult()==null) session.save(doc); else session.update(doc); // Remove properties planned for removal if (doc.getObjectsToRemove().size()>0) { for (int i=0;i<doc.getObjectsToRemove().size();i++) { deleteXWikiObject((BaseObject)doc.getObjectsToRemove().get(i), context, false); } doc.setObjectsToRemove(new ArrayList()); } BaseClass bclass = doc.getxWikiClass(); if (bclass!=null) { bclass.setName(doc.getFullName()); if (bclass.getFieldList().size()>0) saveXWikiClass(bclass, context, false); } else { // TODO: Remove existing class } // TODO: Delete all objects for which we don't have a name in the Map.. Iterator it = doc.getxWikiObjects().values().iterator(); while (it.hasNext()) { Vector objects = (Vector) it.next(); for (int i=0;i<objects.size();i++) { BaseCollection obj = (BaseCollection)objects.get(i); if (obj!=null) saveXWikiCollection(obj, context, false); } } if (bTransaction) { endTransaction(context, true); } doc.setNew(false); } catch (Exception e) { Object[] args = { doc.getFullName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_DOC, "Exception while saving document {0}", e, args); } finally { try { if (bTransaction) { endTransaction(context, false); } } catch (Exception e) {} // End monitoring timer if (monitor!=null) monitor.endTimer("hibernate"); } } public void saveXWikiDoc(XWikiDocument doc, XWikiContext context) throws XWikiException { saveXWikiDoc(doc, context, true); } public XWikiDocument loadXWikiDoc(XWikiDocument doc, XWikiContext context) throws XWikiException { //To change body of implemented methods use Options | File Templates. BufferedReader fr = null; boolean bTransaction = true; MonitorPlugin monitor = getMonitorPlugin(context); try { // Start monitoring timer if (monitor!=null) monitor.startTimer("hibernate"); doc.setStore(this); checkHibernate(context); bTransaction = bTransaction && beginTransaction(context); Session session = getSession(context); try { session.load(doc, new Long(doc.getId())); doc.setNew(false); } catch (ObjectNotFoundException e) { // No document doc.setNew(true); return doc; } // Loading the attachment list loadAttachmentList(doc, context, false); // TODO: handle the case where there are no xWikiClass and xWikiObject in the Database BaseClass bclass = new BaseClass(); bclass.setName(doc.getFullName()); loadXWikiClass(bclass, context, false); doc.setxWikiClass(bclass); Query query; query = session.createQuery("from BaseObject as bobject where bobject.name = :name order by bobject.number"); query.setText("name", doc.getFullName()); Iterator it = query.list().iterator(); boolean hasGroups = false; while (it.hasNext()) { BaseObject object = (BaseObject) it.next(); String className = object.getClassName(); // We use the internal class to store the statistics if (className.equals("internal")) continue; if (className.equals("XWiki.XWikiGroups")) { hasGroups = true; continue; } // It seems to search before is case insensitive // And this would break the loading if we get an // object which doesn't really belong to this document if (!object.getName().equals(doc.getFullName())) continue; if (!className.equals("")) { loadXWikiCollection(object, context, false, true); doc.setObject(className, object.getNumber(), object); } } if (hasGroups) { Query query2; query2 = session.createQuery("select bobject.number, prop.value from StringProperty as prop, BaseObject as bobject where bobject.name = :name and bobject.className='XWiki.XWikiGroups' and bobject.id=prop.id.id and prop.id.name='member' order by bobject.number"); query2.setText("name", doc.getFullName()); Iterator it2 = query2.list().iterator(); while (it2.hasNext()) { Object[] result = (Object[])it2.next(); Integer number = (Integer)result[0]; String member = (String)result[1]; BaseObject obj = new BaseObject(); obj.setName(doc.getFullName()); obj.setClassName("XWiki.XWikiGroups"); obj.setNumber(number.intValue()); obj.setStringValue("member", member); doc.setObject("XWiki.XWikiGroups", obj.getNumber(), obj); } } if (bTransaction) endTransaction(context, false); } catch (Exception e) { Object[] args = { doc.getFullName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_READING_DOC, "Exception while reading document {0}", e, args); } finally { try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} // End monitoring timer if (monitor!=null) monitor.endTimer("hibernate"); } return doc; } private MonitorPlugin getMonitorPlugin(XWikiContext context) { try { if ((context==null)||(context.getWiki()==null)) return null; return (MonitorPlugin) context.getWiki().getPlugin("monitor", context); } catch (Exception e) { return null; } } public XWikiDocument loadXWikiDoc(XWikiDocument basedoc,String version, XWikiContext context) throws XWikiException { XWikiDocument doc = new XWikiDocument(basedoc.getWeb(), basedoc.getName()); MonitorPlugin monitor = getMonitorPlugin(context);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -