1 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 33 public final class EditorDefinitionUtils { 34 35 private EditorDefinitionUtils() { 36 } 37 38 public static ListDefinition getListDefinition( 39 EditorRepository repository, String 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 getObjectId(EditorDefinition def, Object item) { 81 ListDefinition listDef = getListDefinition(def); 82 return listDef.getListConfig().getDao().getObjectId(item); 83 } 84 85 public static Object loadBean(EditorDefinition def, String objectId) { 86 ListDefinition listDef = getListDefinition(def); 87 return listDef.getListConfig().getDao().load(objectId); 88 } 89 90 public static Object getParent(EditorDefinition def, Object 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 getParentId(EditorDefinition def, Object bean) { 101 Object parent = getParent(def, bean); 102 if (parent != null) { 103 return getObjectId(def.getParentEditorDefinition(), parent); 104 } 105 return null; 106 } 107 108 public static Object loadParent(EditorDefinition def, String 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 |