KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > templates > factories > TemplateFactory


1 package com.dotmarketing.portlets.templates.factories;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import com.dotmarketing.beans.Host;
7 import com.dotmarketing.beans.Identifier;
8 import com.dotmarketing.beans.Inode;
9 import com.dotmarketing.db.DotHibernate;
10 import com.dotmarketing.factories.IdentifierFactory;
11 import com.dotmarketing.factories.InodeFactory;
12 import com.dotmarketing.factories.PermissionFactory;
13 import com.dotmarketing.factories.RoleFactory;
14 import com.dotmarketing.factories.WebAssetFactory;
15 import com.dotmarketing.portlets.files.model.File;
16 import com.dotmarketing.portlets.templates.model.Template;
17 import com.dotmarketing.util.Config;
18 import com.dotmarketing.util.Logger;
19 import com.liferay.portal.model.Role;
20
21 /**
22  *
23  * @author will, david (2005)
24  */

25 public class TemplateFactory {
26     public static java.util.List JavaDoc getChildrenTemplateByOrder(Inode i) {
27         return InodeFactory.getChildrenClassByOrder(i, Template.class,
28                 "sort_order");
29     }
30
31     public static java.util.List JavaDoc getActiveTemplates() {
32         DotHibernate dh = new DotHibernate(Template.class);
33         dh
34                 .setQuery("from inode in class com.dotmarketing.portlets.templates.model.Template");
35
36         return dh.list();
37     }
38
39     public static java.util.List JavaDoc getTemplatesByOrderAndParent(String JavaDoc orderby,
40             Inode i) {
41         return InodeFactory.getChildrenClassByOrder(i, Template.class, orderby);
42     }
43
44     public static java.util.List JavaDoc getTemplatesByOrder(String JavaDoc orderby) {
45         DotHibernate dh = new DotHibernate(Template.class);
46         dh
47                 .setQuery("from inode in class com.dotmarketing.portlets.templates.model.Template where working = "
48                         + com.dotmarketing.db.DbConnectionFactory.getDBTrue()
49                         + " or live = "
50                         + com.dotmarketing.db.DbConnectionFactory.getDBTrue()
51                         + " order by " + orderby);
52
53         return dh.list();
54     }
55
56     public static java.util.List JavaDoc getTemplatesByOrderAndWorking(String JavaDoc orderby) {
57         DotHibernate dh = new DotHibernate(Template.class);
58         dh
59                 .setQuery("from inode in class com.dotmarketing.portlets.templates.model.Template where working = "
60                         + com.dotmarketing.db.DbConnectionFactory.getDBTrue()
61                         + " order by " + orderby);
62
63         return dh.list();
64     }
65
66     public static java.util.List JavaDoc getTemplateChildrenByCondition(Inode i,
67             String JavaDoc condition) {
68         return InodeFactory.getChildrenClassByConditionAndOrderBy(i,
69                 Template.class, condition, "title, sort_order");
70     }
71
72     public static java.util.List JavaDoc getTemplateByCondition(String JavaDoc condition) {
73         DotHibernate dh = new DotHibernate(Template.class);
74         dh
75                 .setQuery("from inode in class com.dotmarketing.portlets.templates.model.Template where "
76                         + condition + " order by title, sort_order");
77
78         return dh.list();
79     }
80
81     public static java.util.List JavaDoc getTemplateChildren(Inode i) {
82         return InodeFactory.getChildrenClassByOrder(i, Template.class,
83                 "inode, sort_order");
84     }
85
86     public static Template getTemplateByLiveAndFolderAndTitle(Inode parent,
87             String JavaDoc title) {
88         return (Template) InodeFactory.getChildOfClassbyCondition(parent,
89                 Template.class, " title = '" + title + "' and live = "
90                         + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
91     }
92
93     public static void deleteChildren(Template parent, Class JavaDoc c) {
94         InodeFactory.deleteChildrenOfClass(parent, Template.class);
95     }
96
97     public static java.util.List JavaDoc getTemplatesPerRoleAndCondition(Host host,
98             Role[] roles, String JavaDoc condition) {
99         return getTemplatesPerRoleAndCondition(host.getInode(), roles,
100                 condition);
101     }
102
103     public static java.util.List JavaDoc getTemplatesPerRoleAndCondition(long hostId,
104             Role[] roles, String JavaDoc condition) {
105         java.util.List JavaDoc entries = new java.util.ArrayList JavaDoc();
106
107         java.util.List JavaDoc folders = com.dotmarketing.portlets.folders.factories.FolderFactory
108                 .getFoldersByParent(hostId);
109
110         return com.dotmarketing.portlets.folders.factories.FolderFactory
111                 .getFoldersAndEntriesByRoles(folders, entries, roles,
112                         Template.class, condition);
113     }
114
115     public static java.util.List JavaDoc getTemplatesPerRole(Host host, Role[] roles) {
116         return getTemplatesPerRole(host, roles);
117     }
118
119     public static java.util.List JavaDoc getTemplatesPerRole(long hostId, Role[] roles) {
120         java.util.List JavaDoc entries = new java.util.ArrayList JavaDoc();
121         java.util.List JavaDoc folders = com.dotmarketing.portlets.folders.factories.FolderFactory
122                 .getFoldersByParent(hostId);
123
124         return com.dotmarketing.portlets.folders.factories.FolderFactory
125                 .getFoldersAndEntriesByRoles(folders, entries, roles,
126                         Template.class);
127     }
128
129     public static Template getTemplateParent(Inode o) {
130         try {
131             DotHibernate dh = new DotHibernate(Template.class);
132             dh
133                     .setQuery("from inode in class com.dotmarketing.portlets.templates.model.Template where ? in inode.children.elements and (live="
134                             + com.dotmarketing.db.DbConnectionFactory.getDBTrue()
135                             + " or working="
136                             + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + ")");
137             dh.setParam(o.getInode());
138
139             java.util.List JavaDoc templates = dh.list();
140
141             for (int i = 0; i < templates.size(); i++) {
142                 if (((Template) templates.get(i)).isLive()) {
143                     return (Template) templates.get(i);
144                 }
145             }
146
147             return (Template) templates.get(templates.size());
148         } catch (Exception JavaDoc e) {
149             Logger.warn(TemplateFactory.class, "getTemplateParent failed:" + e,
150                     e);
151         }
152
153         return new Template();
154     }
155
156     public static Template getCategoryTemplate() {
157         try {
158             DotHibernate dh = new DotHibernate(Template.class);
159
160             dh
161                     .setQuery("from inode in class com.dotmarketing.portlets.templates.model.Template where title = ? and working="
162                             + com.dotmarketing.db.DbConnectionFactory.getDBTrue());
163
164             dh.setParam(Config.getStringProperty("CATEGORY_TEMPLATE_NAME"));
165
166             Template t = (Template) dh.load();
167             Logger.debug(TemplateFactory.class, "TEMPLATE = " + t.getInode());
168             return t;
169         } catch (Exception JavaDoc e) {
170             Logger.warn(TemplateFactory.class, "getCategoryTemplate failed:"
171                     + e, e);
172             return null;
173         }
174     }
175
176     public static List JavaDoc getTemplates(com.liferay.portal.model.User user) {
177         java.util.List JavaDoc templates = new ArrayList JavaDoc();
178         try {
179             String JavaDoc condition = "working="
180                     + com.dotmarketing.db.DbConnectionFactory.getDBTrue()
181                     + " and deleted="
182                     + com.dotmarketing.db.DbConnectionFactory.getDBFalse();
183             // gets all user roles
184
com.liferay.portal.model.Role[] roles = (com.liferay.portal.model.Role[]) RoleFactory
185                     .getAllRolesForUser(user.getUserId()).toArray(
186                             new com.liferay.portal.model.Role[0]);
187             int limit = 0;
188             int offset = 0;
189             String JavaDoc orderby = "title";
190             // gets all templates this user has permissions to read
191
templates = WebAssetFactory
192                     .getAssetsAndPermissionsPerRoleAndCondition(roles,
193                             condition, limit, offset, orderby, Template.class,
194                             "template");
195         } catch (Exception JavaDoc e) {
196         }
197         return templates;
198
199     }
200
201     public static List JavaDoc getTemplates(com.liferay.portal.model.Role[] roles) {
202         java.util.List JavaDoc templates = new ArrayList JavaDoc();
203         try {
204             String JavaDoc condition = "working="
205                     + com.dotmarketing.db.DbConnectionFactory.getDBTrue()
206                     + " and deleted="
207                     + com.dotmarketing.db.DbConnectionFactory.getDBFalse();
208             int limit = 0;
209             int offset = 0;
210             String JavaDoc orderby = "title";
211             // gets all templates this user has permissions to read
212
templates = WebAssetFactory
213                     .getAssetsAndPermissionsPerRoleAndCondition(roles,
214                             condition, limit, offset, orderby, Template.class,
215                             "template");
216         } catch (Exception JavaDoc e) {
217         }
218         return templates;
219
220     }
221
222     public static File getImageFile(Template template) {
223         long imageIdentifierInode = template.getImage();
224         Identifier identifier = (Identifier) InodeFactory.getInode(
225                 imageIdentifierInode, Identifier.class);
226         File imageFile = (File) IdentifierFactory.getWorkingChildOfClass(
227                 identifier, File.class);
228         return imageFile;
229     }
230
231     @SuppressWarnings JavaDoc("unchecked")
232     public static Template copyTemplate(Template currentTemplate) {
233
234         Template newTemplate = new Template();
235
236         newTemplate.copy(currentTemplate);
237         newTemplate.setLocked(false);
238         newTemplate.setLive(false);
239         newTemplate.setFriendlyName(currentTemplate.getFriendlyName()
240                 + " (COPY) ");
241         newTemplate.setTitle(currentTemplate.getTitle() + " (COPY) ");
242
243         // persists the webasset
244
InodeFactory.saveInode(newTemplate);
245
246         // gets containers children (we attach identifier to templates instead
247
// of the container inode)
248
java.util.List JavaDoc<Identifier> children = (java.util.List JavaDoc<Identifier>) InodeFactory
249                 .getChildrenClass(currentTemplate, Identifier.class);
250         java.util.Set JavaDoc<Identifier> childrenSet = new java.util.HashSet JavaDoc<Identifier>();
251         childrenSet.addAll(children);
252         
253         for(Identifier id : children ){
254             newTemplate.addChild(id);
255         }
256         
257         //Copy the host
258
Host h = (Host) InodeFactory.getParentOfClass(currentTemplate, Host.class);
259         h.addChild(newTemplate);
260         
261         
262         
263
264         // creates new identifier for this webasset and persists it
265
Identifier newIdentifier = com.dotmarketing.factories.IdentifierFactory
266                 .createNewIdentifier(newTemplate);
267         Logger.debug(TemplateFactory.class, "Parent newIdentifier="
268                 + newIdentifier.getInode());
269         
270         
271         //Copy the host again
272
newIdentifier.setHostInode(h.getInode());
273         // Copy permissions
274
PermissionFactory.copyPermissions(currentTemplate, newTemplate);
275
276         
277         
278         
279         return newTemplate;
280     }
281
282 }
283
Popular Tags