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    
010    public class ClassMetaDAO extends AbstractDAO {
011    
012            public ClassMetaDAO() { }
013    
014            public ClassMetaDAO(PersistenceManager pm) {
015                    super(pm);
016            }
017    
018            public static String getMultiClassMetaOrFilterPart(Map<String, Object> queryParams, Collection<ClassMeta> classMetas) {
019                    StringBuilder result = new StringBuilder();
020    
021                    if (classMetas.size() > 1)
022                            result.append('(');
023    
024                    int idx = -1;
025                    for (ClassMeta classMeta : classMetas) {
026                            if (++idx > 0)
027                                    result.append(" || ");
028    
029                            String classMetaClassIDParamName = "classMeta_classID" + idx;
030                            result.append("this.classMeta_classID == :").append(classMetaClassIDParamName);
031                            queryParams.put(classMetaClassIDParamName, classMeta.getClassID());
032                    }
033    
034                    if (classMetas.size() > 1)
035                            result.append(')');
036    
037                    return result.toString();
038            }
039    
040            public ClassMeta getClassMeta(String packageName, String simpleClassName, boolean throwExceptionIfNotFound)
041            {
042                    Query q = pm.newNamedQuery(ClassMeta.class, ClassMeta.NamedQueries.getClassMetaByPackageNameAndSimpleClassName);
043                    ClassMeta result = (ClassMeta) q.execute(ClassMeta.UNIQUE_SCOPE_CLASS_META, packageName, simpleClassName);
044    
045                    if (result == null && throwExceptionIfNotFound)
046                            throw new JDOObjectNotFoundException(
047                                            "No ClassMeta found for packageName=\"" + packageName + "\" and simpleClassName=\"" + simpleClassName + "\"!"
048                            );
049    
050                    return result;
051            }
052    
053            public ClassMeta getClassMeta(Class<?> clazz, boolean throwExceptionIfNotFound)
054            {
055                    String packageName = clazz.getPackage() == null ? "" : clazz.getPackage().getName();
056                    String simpleClassName = clazz.getSimpleName();
057                    return getClassMeta(packageName, simpleClassName, throwExceptionIfNotFound);
058            }
059    
060            public ClassMeta getClassMeta(long classID, boolean throwExceptionIfNotFound)
061            {
062                    // We cannot use pm.getObjectById(...), because GAE uses a GAE-specific identity
063                    Object identity = ObjectIdFactory.getInstance().createObjectId(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    }