1 24 package org.riotfamily.riot.dao.support; 25 26 import java.util.Collection ; 27 import java.util.List ; 28 29 import org.riotfamily.riot.dao.ListParams; 30 import org.springframework.dao.DataAccessException; 31 import org.springframework.util.Assert; 32 33 37 public class StaticRiotDao extends RiotDaoAdapter { 38 39 private List items; 40 41 private Class entityClass; 42 43 public StaticRiotDao(List items) { 44 Assert.notEmpty(items); 45 this.items = items; 46 } 47 48 public void setEntityClass(Class entityClass) { 49 this.entityClass = entityClass; 50 } 51 52 public Class getEntityClass() { 53 if (entityClass == null) { 54 entityClass = items.get(0).getClass(); 55 } 56 return entityClass; 57 } 58 59 public Collection list(Object parent, ListParams params) { 60 return items; 61 } 62 63 public String getObjectId(Object entity) { 64 int index = items.indexOf(entity); 65 Assert.isTrue(index != -1, "Item not found in list. Make sure " + 66 "hashCode() and equals() are properly implemented."); 67 68 return String.valueOf(index); 69 } 70 71 public Object load(String id) throws DataAccessException { 72 int index = Integer.parseInt(id); 73 return items.get(index); 74 } 75 } 76 | Popular Tags |