1 56 package org.objectstyle.cayenne.pref; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Collections ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 import java.util.Map ; 65 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.DataObjectUtils; 68 import org.objectstyle.cayenne.access.DataContext; 69 import org.objectstyle.cayenne.exp.Expression; 70 import org.objectstyle.cayenne.exp.ExpressionFactory; 71 import org.objectstyle.cayenne.map.DbAttribute; 72 import org.objectstyle.cayenne.map.DbEntity; 73 import org.objectstyle.cayenne.query.SelectQuery; 74 import org.objectstyle.cayenne.util.Util; 75 76 82 public class Domain extends _Domain { 83 84 89 public void rename(String newName) { 90 if (Util.nullSafeEquals(getName(), newName)) { 91 return; 92 } 93 94 Domain parent = getParentDomain(); 95 if (parent == null) { 96 setName(newName); 97 return; 98 } 99 100 Domain other = parent.getSubdomain(newName, false); 101 if (other != null && other != this) { 102 String otherName = null; 103 for (int i = 1; i < 1000; i++) { 104 if (parent.getSubdomain(newName + i, false) == null) { 105 otherName = newName + i; 106 break; 107 } 108 } 109 110 if (otherName == null) { 111 throw new PreferenceException("Can't rename an existing domain '" 112 + newName 113 + "'."); 114 } 115 116 other.setName(otherName); 117 } 118 setName(newName); 119 } 120 121 125 public Domain getSubdomain(Class javaClass) { 126 return getSubdomain(javaClass.getName()); 127 } 128 129 135 public Domain getSubdomain(String subdomainName) { 136 return getSubdomain(subdomainName, true); 137 } 138 139 public Domain getSubdomain(String subdomainName, boolean create) { 140 List subdomains = getSubdomains(); 141 142 if (subdomains.size() > 0) { 143 List matching = ExpressionFactory.matchExp( 144 Domain.NAME_PROPERTY, 145 subdomainName).filterObjects(subdomains); 146 if (matching.size() > 0) { 147 return (Domain) matching.get(0); 148 } 149 } 150 151 if (!create) { 152 return null; 153 } 154 155 Domain childSubdomain = (Domain) getDataContext().createAndRegisterNewObject( 156 Domain.class); 157 addToSubdomains(childSubdomain); 158 childSubdomain.setName(subdomainName); 159 160 if (getLevel() == null) { 161 throw new CayenneRuntimeException("Null level, can't create child"); 162 } 163 164 int level = getLevel().intValue() + 1; 165 childSubdomain.setLevel(new Integer (level)); 166 getDataContext().commitChanges(); 167 168 return childSubdomain; 169 } 170 171 174 public List getDetails() { 175 Collection domainPrefs = getPreferences(); 176 177 if (domainPrefs.isEmpty()) { 178 return new ArrayList (1); 180 } 181 182 List details = new ArrayList (domainPrefs.size()); 183 Iterator it = domainPrefs.iterator(); 184 while (it.hasNext()) { 185 DomainPreference preference = (DomainPreference) it.next(); 186 details.add(preference.getPreference()); 187 } 188 return details; 189 } 190 191 194 public PreferenceDetail getDetail(String key, boolean create) { 195 return getDetail(key, null, create); 196 } 197 198 201 public Collection getDetails(Class javaClass) { 202 Collection preferences = getPreferences(); 204 if (preferences.isEmpty()) { 205 return Collections.EMPTY_LIST; 206 } 207 208 Collection ids = new ArrayList (preferences.size()); 209 Iterator it = preferences.iterator(); 210 while (it.hasNext()) { 211 DomainPreference pref = (DomainPreference) it.next(); 212 ids.add(DataObjectUtils.pkForObject(pref)); 213 } 214 215 DataContext context = getDataContext(); 216 DbEntity entity = context.getEntityResolver().lookupDbEntity(javaClass); 217 DbAttribute pk = (DbAttribute) entity.getPrimaryKey().get(0); 218 219 Expression qualifier = Expression.fromString("db:" + pk.getName() + " in $ids"); 220 Map params = Collections.singletonMap("ids", ids); 221 SelectQuery query = new SelectQuery(javaClass, qualifier 222 .expWithParameters(params)); 223 return context.performQuery(query); 224 } 225 226 229 public Map getDetailsMap(Class javaClass) { 230 Collection details = getDetails(javaClass); 231 Map map = new HashMap (); 232 233 if (details.isEmpty()) { 234 return map; 235 } 236 237 Iterator it = details.iterator(); 238 while (it.hasNext()) { 239 PreferenceDetail detail = (PreferenceDetail) it.next(); 240 map.put(detail.getKey(), detail); 241 } 242 243 return map; 244 } 245 246 252 public PreferenceDetail getDetail(String key, Class javaClass, boolean create) { 253 DomainPreference preferenceLink = getDomainPreference(key); 254 255 if (preferenceLink == null) { 256 257 if (!create) { 258 return null; 259 } 260 261 preferenceLink = (DomainPreference) getDataContext() 262 .createAndRegisterNewObject(DomainPreference.class); 263 preferenceLink.setDomain(this); 264 preferenceLink.setKey(key); 265 getDataContext().commitChanges(); 266 } 267 268 return (javaClass == null) ? preferenceLink.getPreference() : preferenceLink 269 .getPreference(javaClass, create); 270 } 271 272 275 DomainPreference getDomainPreference(String key) { 276 Map params = new HashMap (); 279 params.put("key", key); 280 params.put("domain", this); 281 List results = getDataContext().performQuery( 282 "DomainPreferenceForKey", 283 params, 284 false); 285 return (results.size() < 1) ? null : (DomainPreference) results.get(0); 286 } 287 } 288 289 | Popular Tags |