001 package org.cumulus4j.store.model; 002 003 import java.util.Collection; 004 import java.util.Map; 005 006 import javax.jdo.JDOObjectNotFoundException; 007 import javax.jdo.PersistenceManager; 008 import javax.jdo.Query; 009 import javax.jdo.identity.LongIdentity; 010 011 public class ClassMetaDAO extends AbstractDAO { 012 013 public ClassMetaDAO() { } 014 015 public ClassMetaDAO(PersistenceManager pm) { 016 super(pm); 017 } 018 019 public static String getMultiClassMetaOrFilterPart(Map<String, Object> queryParams, Collection<ClassMeta> classMetas) { 020 StringBuilder result = new StringBuilder(); 021 022 if (classMetas.size() > 1) 023 result.append('('); 024 025 int idx = -1; 026 for (ClassMeta classMeta : classMetas) { 027 if (++idx > 0) 028 result.append(" || "); 029 030 String classMetaClassIDParamName = "classMeta_classID" + idx; 031 result.append("this.classMeta_classID == :").append(classMetaClassIDParamName); 032 queryParams.put(classMetaClassIDParamName, classMeta.getClassID()); 033 } 034 035 if (classMetas.size() > 1) 036 result.append(')'); 037 038 return result.toString(); 039 } 040 041 public ClassMeta getClassMeta(String packageName, String simpleClassName, boolean throwExceptionIfNotFound) 042 { 043 Query q = pm.newNamedQuery(ClassMeta.class, ClassMeta.NamedQueries.getClassMetaByPackageNameAndSimpleClassName); 044 ClassMeta result = (ClassMeta) q.execute(ClassMeta.UNIQUE_SCOPE_CLASS_META, packageName, simpleClassName); 045 046 if (result == null && throwExceptionIfNotFound) 047 throw new JDOObjectNotFoundException( 048 "No ClassMeta found for packageName=\"" + packageName + "\" and simpleClassName=\"" + simpleClassName + "\"!" 049 ); 050 051 return result; 052 } 053 054 public ClassMeta getClassMeta(Class<?> clazz, boolean throwExceptionIfNotFound) 055 { 056 String packageName = clazz.getPackage() == null ? "" : clazz.getPackage().getName(); 057 String simpleClassName = clazz.getSimpleName(); 058 return getClassMeta(packageName, simpleClassName, throwExceptionIfNotFound); 059 } 060 061 public ClassMeta getClassMeta(long classID, boolean throwExceptionIfNotFound) 062 { 063 Object identity = new LongIdentity(ClassMeta.class, classID); 064 try { 065 ClassMeta classMeta = (ClassMeta) pm.getObjectById(identity); 066 return classMeta; 067 } catch (JDOObjectNotFoundException x) { 068 if (throwExceptionIfNotFound) 069 throw x; 070 else 071 return null; 072 } 073 } 074 075 public EmbeddedClassMeta getEmbeddedClassMeta(FieldMeta embeddingFieldMeta, boolean throwExceptionIfNotFound) { 076 Query q = pm.newNamedQuery(EmbeddedClassMeta.class, EmbeddedClassMeta.NamedQueries.getEmbeddedClassMetaByEmbeddingFieldMeta_fieldID); 077 EmbeddedClassMeta result = (EmbeddedClassMeta) q.execute(embeddingFieldMeta.getFieldID()); 078 079 // EmbeddedClassMeta result; 080 // @SuppressWarnings("unchecked") 081 // Collection<EmbeddedClassMeta> c = (Collection<EmbeddedClassMeta>) q.execute(embeddingFieldMeta); 082 // if (c.size() == 1) 083 // result = c.iterator().next(); 084 // else if (c.isEmpty()) 085 // result = null; 086 // else 087 // throw new IllegalStateException("Found multiple EmbeddeClassMeta instances: " + c); 088 089 if (result == null && throwExceptionIfNotFound) 090 throw new JDOObjectNotFoundException( 091 "No EmbeddedClassMeta found with embeddingFieldMeta=\"" + embeddingFieldMeta + "\"!" 092 ); 093 094 return result; 095 } 096 }