View Javadoc

1   package net.sf.cantina.datasource;
2   
3   import net.sf.cantina.Document;
4   import net.sf.cantina.Realm;
5   
6   import java.util.*;
7   
8   /***
9    * @author Stephane JAIS
10   * @hibernate.class table="documents"
11   */
12  public class PersistentDocument
13    implements Document
14  {
15    public static final String ENCODING = "UTF-8";
16    private String documentId;
17    private Realm realm;
18    private String contentType;
19    private Map translations;
20  
21    public PersistentDocument()
22    {
23    }
24  
25    /***
26     * Gets the primary key.
27     *
28     * @return The primary key
29     * @hibernate.id
30     * generator-class="assigned"
31     * length="255"
32     * type="string"
33     * column="document_id"
34     */
35  
36    public String getDocumentId()
37    {
38      return documentId;
39    }
40  
41    public void setDocumentId(String documentId)
42    {
43      this.documentId = documentId;
44    }
45  
46    /***
47     * Gets the realm for this document
48     *
49     * @return the Realm
50     * @hibernate.many-to-one column="realm"
51     * class="net.sf.cantina.datasource.PersistentRealm"
52     * cascade="none"
53     * insert="true"
54     */
55  
56    public Realm getRealm()
57    {
58      return realm;
59    }
60  
61    /***
62     * sets the realm for this document
63     *
64     * @param realm
65     */
66  
67    public void setRealm(Realm realm)
68    {
69      this.realm = realm;
70    }
71  
72    /***
73     * Returns the locales available for this document.
74     */
75  
76    public Collection getAvailableLocales()
77    {
78      Vector v = new Vector();
79      for (Iterator i = getTranslations().keySet().iterator(); i.hasNext();)
80      {
81        Locale l = (Locale)i.next();
82        if (getContent(l).length > 0)
83          v.add(l);
84      }
85      return v;
86    }
87  
88    /***
89     * @hibernate.collection-one-to-many class="net.sf.cantina.datasource.Translation"
90     * @hibernate.collection-key type="string"
91     * column="document_id"
92     * @hibernate.collection-index type="java.util.Locale"
93     * column="locale"
94     * @hibernate.map cascade="all-delete-orphan"
95     */
96  
97    public Map getTranslations()
98    {
99      if (this.translations == null)
100       this.translations = new HashMap();
101     return this.translations;
102   }
103 
104   public void setTranslations(Map translations)
105   {
106     this.translations = translations;
107   }
108 
109   public Translation getTranslation(Locale locale)
110   {
111     return (Translation) getTranslations().get(locale);
112   }
113 
114   public byte[] getContent(Locale locale)
115   {
116     if (getTranslation(locale) != null)
117       return getTranslation(locale).getContent();
118     return new byte[] {};
119   }
120 
121   public String getContentAsString(Locale locale)
122   throws Exception
123   {
124     return new String(getContent(locale),ENCODING);
125   }
126 
127   public void setContent(Locale locale,byte[] content)
128   {
129     Translation tr = getTranslation(locale);
130     if (tr == null)
131       tr = new Translation(locale);
132     tr.setContent(content == null ? null : content);
133     getTranslations().put(tr.getLocale(), tr);
134   }
135 
136   public void setContentAsString(Locale locale, String content)
137   throws Exception
138   {
139     setContent(locale,content.getBytes(ENCODING));
140   }
141 
142   /***
143    * @hibernate.property
144    *   column="content_type"
145    */
146 
147   public String getContentType()
148   {
149     return contentType;
150   }
151 
152   public void setContentType(String contentType)
153   {
154     this.contentType = contentType;
155   }
156 
157 }