View Javadoc
1   package net.sf.maia.core.storage;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   
6   import javax.jcr.ImportUUIDBehavior;
7   import javax.jcr.Node;
8   import javax.jcr.PathNotFoundException;
9   import javax.jcr.Property;
10  import javax.jcr.RepositoryException;
11  import javax.jcr.Session;
12  
13  import net.sf.maia.Page;
14  
15  /***
16   *
17   *
18   * @author Magnus Grimsell
19   */
20  public class PageStore
21  {
22      private final static String CONTENT = "maia:content";
23  
24      private SessionHolder mSessionHolder;
25      
26      public SessionHolder getSessionHolder()
27      {
28          return mSessionHolder;
29      }
30      
31      public void setSessionHolder(SessionHolder sessionHolder)
32      {
33          mSessionHolder = sessionHolder;
34      }
35      
36      /***
37       * Returns a wiki page from the page store.
38       * @param path Path to the page
39       * @return the page or null if the page does not exist
40       */
41      public Page getPage(String path)
42      {
43          Session session = null;
44          Page page = new Page();
45          try
46          {
47              session = mSessionHolder.getSession();
48              Node n = (Node)session.getItem(path);
49              Property p = n.getProperty(CONTENT);
50              page.setContent(p.getString());
51              page.setType(n.getPrimaryNodeType().getName());
52              page.setName(path);
53          }
54          catch(PathNotFoundException e)
55          {
56              // ok no page was found at the given path
57          }
58          catch(RepositoryException e)
59          {
60              throw new StorageException(e);
61          }
62          finally
63          {
64              if(session != null)
65              {
66                  session.logout();
67              }
68          }
69          return page;
70      }
71      
72      public void importXML(InputStream in)
73      {
74          Session session = null;
75          try
76          {
77              session = mSessionHolder.getSession();
78              session.importXML("/", in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
79              session.save();
80          }
81          catch(RepositoryException e)
82          {
83              throw new StorageException(e);
84          }
85          catch(IOException e)
86          {
87              throw new StorageException(e);
88          }
89          finally
90          {
91              if(session != null)
92              {
93                  session.logout();
94              }
95          }
96      }
97  }