Package org.jpox.metadata

Examples of org.jpox.metadata.QueryMetaData


            throw new IllegalArgumentException(LOCALISER.msg("Query.NamedQueryNotFound", queryName));
        }

        // Find the Query for the specified class
        ClassLoaderResolver clr = om.getClassLoaderResolver();
        QueryMetaData qmd = om.getMetaDataManager().getMetaDataForQuery(null, clr, queryName);
        if (qmd == null)
        {
            throw new IllegalArgumentException(LOCALISER.msg("Query.NamedQueryNotFound", queryName));
        }

        // Create the Query
        try
        {
            if (qmd.getLanguage().equals(QueryLanguage.JPQL))
            {
                // "named-query" so return JPQL
                org.jpox.store.query.Query jpoxQuery = getObjectManager().getOMFContext().getQueryManager().newQuery(
                    qmd.getLanguage().toString(), getObjectManager(), qmd.getQuery());
                return new JPAQuery(this, jpoxQuery, qmd.getLanguage().toString());
            }
            else if (qmd.getLanguage().equals(QueryLanguage.SQL))
            {
                // "named-native-query" so return SQL
                org.jpox.store.query.Query jpoxQuery = getObjectManager().getOMFContext().getQueryManager().newQuery(
                    qmd.getLanguage().toString(), getObjectManager(), qmd.getQuery());
                if (qmd.getResultClass() != null)
                {
                    // Named SQL query with result class
                    String resultClassName = qmd.getResultClass();
                    Class resultClass = null;
                    try
                    {
                        resultClass = om.getClassLoaderResolver().classForName(resultClassName);
                        jpoxQuery.setResultClass(resultClass);
                        return new JPAQuery(this, jpoxQuery, qmd.getLanguage().toString());
                    }
                    catch (Exception e)
                    {
                        // Result class not found so throw exception (not defined in the JPA spec)
                        throw new IllegalArgumentException(LOCALISER.msg("Query.ResultClassNotFound", qmd.getName(), resultClassName));
                    }
                }
                else if (qmd.getResultMetaDataName() != null)
                {
                    QueryResultMetaData qrmd = om.getMetaDataManager().getMetaDataForQueryResult(qmd.getResultMetaDataName());
                    if (qrmd == null)
                    {
                        // TODO Localise this, and check if it is the correct exception to throw
                        throw new IllegalArgumentException("ResultSetMapping " + qmd.getResultMetaDataName() + " is not found");
                    }
                    jpoxQuery.setResultMetaData(qrmd);
                    return new JPAQuery(this, jpoxQuery, qmd.getLanguage().toString());
                }
                else
                {
                    return new JPAQuery(this, jpoxQuery, qmd.getLanguage().toString());
                }
            }
            else
            {
                throw new IllegalArgumentException(LOCALISER.msg("Query.LanguageNotSupportedByStore", qmd.getLanguage()));
            }
        }
        catch (JPOXException jpe)
        {
            throw new IllegalArgumentException(jpe.getMessage(), jpe);
View Full Code Here


                // Named JPQL query
                MetaData md = getStack();
                if (md instanceof FileMetaData)
                {
                    FileMetaData filemd = (FileMetaData)md;
                    QueryMetaData qmd = new QueryMetaData(filemd, null, getAttr(attrs, "name"), "javax.jdo.query.JPQL",
                        null, null, null, null, null);
                    filemd.addQuery(qmd);
                    pushStack(qmd);
                }
                else if (md instanceof ClassMetaData)
                {
                    ClassMetaData cmd = (ClassMetaData)md;
                    QueryMetaData qmd = new QueryMetaData(cmd, null, getAttr(attrs, "name"), "javax.jdo.query.JPQL",
                        null, null, null, null, null);
                    cmd.addQuery(qmd);
                    pushStack(qmd);
                }
            }
            else if (localName.equals("named-native-query"))
            {
                // Named SQL query
                MetaData md = getStack();
                if (md instanceof FileMetaData)
                {
                    FileMetaData filemd = (FileMetaData)md;
                    QueryMetaData qmd = new QueryMetaData(filemd, null, getAttr(attrs, "name"), "javax.jdo.query.SQL", null,
                        getAttr(attrs, "result-class"), getAttr(attrs, "result-set-mapping"), null, null);
                    filemd.addQuery(qmd);
                    pushStack(qmd);
                }
                else if (md instanceof ClassMetaData)
                {
                    ClassMetaData cmd = (ClassMetaData)md;
                    QueryMetaData qmd = new QueryMetaData(cmd, null, getAttr(attrs, "name"), "javax.jdo.query.SQL", null,
                        getAttr(attrs, "result-class"), getAttr(attrs, "result-set-mapping"), null, null);
                    cmd.addQuery(qmd);
                    pushStack(qmd);
                }
            }
View Full Code Here

                        {
                            namedQueries = new HashSet<QueryMetaData>();
                        }
                        for (int j=0;j<queries.length;j++)
                        {
                            QueryMetaData qmd = new QueryMetaData(cmd, null, queries[j].name(), QueryLanguage.JPQL.toString(),
                                "true", null, null, null, null);
                            qmd.setQuery(queries[j].query());
                            namedQueries.add(qmd);
                        }
                    }
                    else if (annName.equals(JPAAnnotationUtils.NAMED_QUERY))
                    {
                        if (namedQueries == null)
                        {
                            namedQueries = new HashSet<QueryMetaData>();
                        }
                        QueryMetaData qmd = new QueryMetaData(cmd, null, (String)annotationValues.get("name"),
                            QueryLanguage.JPQL.toString(), "true", null, null, null, null);
                        qmd.setQuery((String)annotationValues.get("query"));
                        namedQueries.add(qmd);
                    }
                    else if (annName.equals(JPAAnnotationUtils.NAMED_NATIVE_QUERIES))
                    {
                        NamedNativeQuery[] queries = (NamedNativeQuery[])annotationValues.get("value");
                        if (namedQueries == null)
                        {
                            namedQueries = new HashSet<QueryMetaData>();
                        }
                        for (int j=0;j<queries.length;j++)
                        {
                            String resultClassName = null;
                            if (queries[j].resultClass() != null && queries[j].resultClass() != void.class)
                            {
                                resultClassName = queries[j].resultClass().getName();
                            }
                            String resultMappingName = null;
                            if (queries[j].resultSetMapping() != null)
                            {
                                resultMappingName = queries[j].resultSetMapping();
                            }
                            QueryMetaData qmd = new QueryMetaData(cmd, null, queries[j].name(), QueryLanguage.SQL.toString(),
                                "true", resultClassName, resultMappingName, null, null);
                            qmd.setQuery(queries[j].query());
                            namedQueries.add(qmd);
                        }
                    }
                    else if (annName.equals(JPAAnnotationUtils.NAMED_NATIVE_QUERY))
                    {
                        if (namedQueries == null)
                        {
                            namedQueries = new HashSet<QueryMetaData>();
                        }

                        Class resultClass = (Class)annotationValues.get("resultClass");
                        String resultClassName = null;
                        if (resultClass != null && resultClass != void.class)
                        {
                            resultClassName = resultClass.getName();
                        }
                        String resultMappingName = (String)annotationValues.get("resultSetMapping");
                        if (StringUtils.isWhitespace(resultMappingName))
                        {
                            resultMappingName = null;
                        }
                        QueryMetaData qmd = new QueryMetaData(cmd, null, (String)annotationValues.get("name"),
                            QueryLanguage.SQL.toString(), "true", resultClassName, resultMappingName, null, null);
                        qmd.setQuery((String)annotationValues.get("query"));
                        namedQueries.add(qmd);
                    }
                    else if (annName.equals(JPAAnnotationUtils.SQL_RESULTSET_MAPPINGS))
                    {
                        SqlResultSetMapping[] mappings = (SqlResultSetMapping[])annotationValues.get("value");
View Full Code Here

                        for (int j=0;j<queries.length;j++)
                        {
                            String lang = JDOAnnotationUtils.getQueryLanguageName(qs[j].language());
                            String resultClassName = (qs[j].resultClass() != null && qs[j].resultClass() != void.class ?
                                    qs[j].resultClass().getName() : null);
                            queries[j] = new QueryMetaData(cmd, cls.getName(), qs[j].name(), lang,
                                "" + qs[j].unmodifiable(), resultClassName, null, qs[j].unique(), qs[j].fetchPlan());
                            queries[j].setQuery(qs[j].value());
                            JDOAnnotationUtils.addExtensionsToMetaData(queries[j], qs[j].extensions());
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.QUERY))
                    {
                        if (queries != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.QuerySpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        queries = new QueryMetaData[1];
                        String unmodifiable = "" + annotationValues.get("unmodifiable");
                        Class resultClassValue = (Class)annotationValues.get("resultClass");
                        String resultClassName =
                            (resultClassValue != null && resultClassValue != void.class ? resultClassValue.getName() : null);
                        String lang = JDOAnnotationUtils.getQueryLanguageName((String)annotationValues.get("language"));
                        queries[0] = new QueryMetaData(cmd, cls.getName(), (String)annotationValues.get("name"),
                            lang.toString(), unmodifiable, resultClassName, null, (String)annotationValues.get("unique"),
                            (String)annotationValues.get("fetchPlan"));
                        queries[0].setQuery((String)annotationValues.get("value"));
                        JDOAnnotationUtils.addExtensionsToMetaData(queries[0], (Extension[])annotationValues.get("extensions"));
                    }
View Full Code Here

     * @param queryName Name of the query
     * @return The QueryMetaData for the query for this class
     **/
    public QueryMetaData getMetaDataForQuery(Class cls, ClassLoaderResolver clr, String queryName)
    {
        QueryMetaData qmd = super.getMetaDataForQuery(cls, clr, queryName);
        if (qmd != null)
        {
            return qmd;
        }

View Full Code Here

            }
            // New query for this class
            else if (localName.equals("query"))
            {
                MetaData emd = getStack();
                QueryMetaData qmd = new QueryMetaData(emd,
                                (emd instanceof ClassMetaData ? ((ClassMetaData)emd).getFullClassName() : null),
                                getAttr(attrs, "name"),
                                getAttr(attrs, "language"),
                                getAttr(attrs, "unmodifiable"),
                                getAttr(attrs, "result-class"),
View Full Code Here

            throw new JDOUserException(LOCALISER_JDO.msg("011005", queryName, cls));
        }

        // Find the Query for the specified class
        ClassLoaderResolver clr = objectMgr.getClassLoaderResolver();
        QueryMetaData qmd = objectMgr.getMetaDataManager().getMetaDataForQuery(cls, clr, queryName);
        if (qmd == null)
        {
            throw new JDOUserException(LOCALISER_JDO.msg("011005", queryName, cls));
        }

        // Create the Query
        Query query = newQuery(qmd.getLanguage().toString(), qmd.getQuery());
        if (cls != null)
        {
            query.setClass(cls);
            if (!objectMgr.getStoreManager().managesClass(cls.getName()))
            {
                // Load the candidate class since not yet managed
                objectMgr.getStoreManager().addClass(cls.getName(), clr);
            }
        }

        // Optional args that should only be used with SQL
        if (qmd.getLanguage() == QueryLanguage.JDOQL && (qmd.isUnique() || qmd.getResultClass() != null))
        {
            throw new JDOUserException(LOCALISER_JDO.msg("011007", queryName));
        }
        if (qmd.isUnique())
        {
            query.setUnique(true);
        }
        if (qmd.getResultClass() != null)
        {
            // Set the result class, allowing for it being in the same package as the candidate
            Class resultCls = null;
            try
            {
                resultCls = clr.classForName(qmd.getResultClass());
            }
            catch (ClassNotResolvedException cnre)
            {
                try
                {
                    String resultClassName = cls.getPackage().getName() + "." + qmd.getResultClass();
                    resultCls = clr.classForName(resultClassName);
                }
                catch (ClassNotResolvedException cnre2)
                {
                    throw new JDOUserException(LOCALISER_JDO.msg("011008", queryName, qmd.getResultClass()));
                }
            }
            query.setResultClass(resultCls);
        }

        // Add any JPOX extensions
        if (qmd.getLanguage() == QueryLanguage.JPOXSQL)
        {
            // Apply any imports specified
            if (qmd.hasExtension("imports"))
            {
                query.declareImports(qmd.getValueForExtension("imports"));
            }
            // Apply any parameters specified
            if (qmd.hasExtension("parameters"))
            {
                query.declareParameters(qmd.getValueForExtension("parameters"));
            }
        }
        if (qmd.isUnmodifiable())
        {
            query.setUnmodifiable();
        }
        if (qmd.getFetchPlanName() != null)
        {
            // Apply any named FetchPlan to the query
            FetchPlanMetaData fpmd =
                getObjectManager().getMetaDataManager().getMetaDataForFetchPlan(qmd.getFetchPlanName());
            if (fpmd != null)
            {
                org.jpox.FetchPlan fp = new org.jpox.FetchPlan(apmf, clr);
                fp.removeGroup(org.jpox.FetchPlan.DEFAULT);
                FetchGroupMetaData[] fgmds = fpmd.getFetchGroupMetaData();
View Full Code Here

TOP

Related Classes of org.jpox.metadata.QueryMetaData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.