001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.riot.editor;
025:
026: import org.riotfamily.riot.dao.ParentChildDao;
027: import org.riotfamily.riot.dao.RiotDao;
028: import org.springframework.util.Assert;
029:
030: /**
031: *
032: */
033: public final class EditorDefinitionUtils {
034:
035: private EditorDefinitionUtils() {
036: }
037:
038: public static ListDefinition getListDefinition(
039: EditorRepository repository, String editorId) {
040:
041: EditorDefinition def = repository.getEditorDefinition(editorId);
042: return getListDefinition(def);
043: }
044:
045: public static ListDefinition getListDefinition(EditorDefinition def) {
046: if (def instanceof ListDefinition) {
047: return (ListDefinition) def;
048: } else {
049: return getParentListDefinition(def);
050: }
051: }
052:
053: public static ListDefinition getParentListDefinition(
054: EditorDefinition def) {
055: if (def == null) {
056: return null;
057: }
058: EditorDefinition parentDef = def.getParentEditorDefinition();
059: while (parentDef != null) {
060: if (parentDef instanceof ListDefinition) {
061: return (ListDefinition) parentDef;
062: }
063: parentDef = parentDef.getParentEditorDefinition();
064: }
065: return null;
066: }
067:
068: public static ListDefinition getRootListDefinition(
069: EditorDefinition def) {
070: ListDefinition list = getListDefinition(def);
071: ListDefinition parent = list;
072: while (parent != null) {
073: list = parent;
074: parent = getParentListDefinition(parent);
075: }
076: return list;
077: }
078:
079: public static ListDefinition getNextListDefinition(
080: ListDefinition start, ListDefinition destination) {
081:
082: if (start instanceof TreeDefinition.NodeListDefinition) {
083: start = ((TreeDefinition.NodeListDefinition) start)
084: .getTreeDefinition();
085: }
086: ListDefinition def = destination;
087: ListDefinition parent = getParentListDefinition(def);
088: while (parent != start && parent != null) {
089: def = parent;
090: parent = getParentListDefinition(def);
091: }
092: return def;
093: }
094:
095: public static String getObjectId(EditorDefinition def, Object item) {
096: ListDefinition listDef = getListDefinition(def);
097: return listDef.getListConfig().getDao().getObjectId(item);
098: }
099:
100: public static Object loadBean(EditorDefinition def, String objectId) {
101: ListDefinition listDef = getListDefinition(def);
102: return listDef.getListConfig().getDao().load(objectId);
103: }
104:
105: public static Object getParent(EditorDefinition def, Object bean) {
106: ListDefinition listDef = getListDefinition(def);
107: RiotDao dao = listDef.getListConfig().getDao();
108: if (dao instanceof ParentChildDao) {
109: ParentChildDao parentChildDao = (ParentChildDao) dao;
110: return parentChildDao.getParent(bean);
111: }
112: return null;
113: }
114:
115: public static String getParentId(EditorDefinition def, Object bean) {
116: Object parent = getParent(def, bean);
117: if (parent != null) {
118: return getObjectId(def.getParentEditorDefinition(), parent);
119: }
120: return null;
121: }
122:
123: public static Object loadParent(EditorDefinition def,
124: String parentId) {
125: if (parentId != null) {
126: ListDefinition listDef = getParentListDefinition(def);
127: Assert.notNull(listDef,
128: "No parent ListDefinition found for editor "
129: + def.getId());
130:
131: return loadBean(listDef, parentId);
132: }
133: return null;
134: }
135: }
|