KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > editor > EditorDefinitionUtils


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.editor;
25
26 import org.riotfamily.riot.dao.ParentChildDao;
27 import org.riotfamily.riot.dao.RiotDao;
28 import org.springframework.util.Assert;
29
30 /**
31  *
32  */

33 public final class EditorDefinitionUtils {
34
35     private EditorDefinitionUtils() {
36     }
37     
38     public static ListDefinition getListDefinition(
39             EditorRepository repository, String JavaDoc editorId) {
40         
41         EditorDefinition def = repository.getEditorDefinition(editorId);
42         return getListDefinition(def);
43     }
44     
45     public static ListDefinition getListDefinition(EditorDefinition def) {
46         if (def instanceof ListDefinition) {
47             return (ListDefinition) def;
48         }
49         else {
50             return getParentListDefinition(def);
51         }
52     }
53         
54     public static ListDefinition getParentListDefinition(EditorDefinition def) {
55         if (def == null) {
56             return null;
57         }
58         EditorDefinition parentDef = def.getParentEditorDefinition();
59         while (parentDef != null) {
60             if (parentDef instanceof ListDefinition) {
61                 return (ListDefinition) parentDef;
62             }
63             parentDef = parentDef.getParentEditorDefinition();
64         }
65         return null;
66     }
67     
68     public static ListDefinition getNextListDefinition(
69             ListDefinition start, ListDefinition destination) {
70         
71         ListDefinition def = destination;
72         ListDefinition parent = getParentListDefinition(def);
73         while (parent != start && parent != null) {
74             def = parent;
75             parent = getParentListDefinition(def);
76         }
77         return def;
78     }
79
80     public static String JavaDoc getObjectId(EditorDefinition def, Object JavaDoc item) {
81         ListDefinition listDef = getListDefinition(def);
82         return listDef.getListConfig().getDao().getObjectId(item);
83     }
84     
85     public static Object JavaDoc loadBean(EditorDefinition def, String JavaDoc objectId) {
86         ListDefinition listDef = getListDefinition(def);
87         return listDef.getListConfig().getDao().load(objectId);
88     }
89
90     public static Object JavaDoc getParent(EditorDefinition def, Object JavaDoc bean) {
91         ListDefinition listDef = getListDefinition(def);
92         RiotDao dao = listDef.getListConfig().getDao();
93         if (dao instanceof ParentChildDao) {
94             ParentChildDao parentChildDao = (ParentChildDao) dao;
95             return parentChildDao.getParent(bean);
96         }
97         return null;
98     }
99     
100     public static String JavaDoc getParentId(EditorDefinition def, Object JavaDoc bean) {
101         Object JavaDoc parent = getParent(def, bean);
102         if (parent != null) {
103             return getObjectId(def.getParentEditorDefinition(), parent);
104         }
105         return null;
106     }
107     
108     public static Object JavaDoc loadParent(EditorDefinition def, String JavaDoc parentId) {
109         if (parentId != null) {
110             ListDefinition listDef = getParentListDefinition(def);
111             Assert.notNull(listDef, "No parent ListDefinition found for editor "
112                     + def.getId());
113             
114             return loadBean(listDef, parentId);
115         }
116         return null;
117     }
118 }
119
Popular Tags