001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency;
010:
011: /**
012: * Represents concrete query factory. It uses QueryFactoryBase delegate class as as supporting
013: * "engine" to perform new BasicQuery object instatiation.
014: *
015: * @author Gennady Krizhevsky
016: */
017: public interface BasicManagedQueryFactory extends QueryFactoryBase {
018:
019: /**
020: * One of the SupportedDelegates static fields
021: *
022: * @return delegate factory class
023: */
024: Class getDelegateType();
025:
026: /**
027: * Sets delegate factory
028: *
029: * @param queryFactory delegate factory
030: */
031: void setDelegate(QueryFactoryBase queryFactory);
032:
033: /**
034: * Sets default parameters that will be set when query is instantiated
035: *
036: * @param parameters default parameters that will be set when query is instantiated
037: */
038: void setDefaultParameters(Parameter[] parameters);
039:
040: /**
041: * Contains supported QueryFactoryBase delegate class validations.
042: */
043: static final class SupportedDelegates {
044: public static final Class CALL_FACTORY = CallFactory.class;
045: public static final Class QUERY_FACTORY = QueryFactory.class;
046:
047: private static Class[] SUPPORTED = new Class[] { CALL_FACTORY,
048: QUERY_FACTORY, };
049:
050: /**
051: * Checks if factoryClass is a valid delegate.
052: *
053: * @param factoryDelegateClass
054: * @throws OdalRuntimePersistencyException if factoryClass is not a valid delegate
055: */
056: public static void validate(Class factoryDelegateClass) {
057: boolean found = false;
058: for (int i = 0; i < SUPPORTED.length; i++) {
059: Class aClass = SUPPORTED[i];
060: if (aClass.isAssignableFrom(factoryDelegateClass)) {
061: found = true;
062: break;
063: }
064: }
065: if (!found) {
066: invalidateFactoryClass(factoryDelegateClass);
067: }
068: }
069:
070: private static void invalidateFactoryClass(
071: Class factoryDelegateClass) {
072: throw new OdalRuntimePersistencyException(
073: "Unsupported delegate factory: "
074: + factoryDelegateClass + "; supported: "
075: + SUPPORTED);
076: }
077:
078: /**
079: * Returns true if factory delegate class is of CallFactory type
080: *
081: * @param factoryDelegateClass
082: * @return true if factory delegate class is of CallFactory type
083: */
084: public static boolean isCall(Class factoryDelegateClass) {
085: return CALL_FACTORY.isAssignableFrom(factoryDelegateClass);
086: }
087:
088: /**
089: * Returns true if factory delegate class is of QueryFactory type
090: *
091: * @param factoryDelegateClass
092: * @return true if factory delegate class is of QueryFactory type
093: */
094: public static boolean isQuery(Class factoryDelegateClass) {
095: return QUERY_FACTORY.isAssignableFrom(factoryDelegateClass);
096: }
097:
098: /**
099: * Returns appropriate QueryFactoryBase delegate from Persistency2 based on concrete factory type
100: *
101: * @param factory
102: * @param persistency
103: * @return appropriate QueryFactoryBase delegate from Persistency2 based on concrete factory type
104: */
105: public static QueryFactoryBase selectDelegate(
106: BasicManagedQueryFactory factory,
107: Persistency2 persistency) {
108: if (factory == null) {
109: throw new NullPointerException("factory == null");
110: }
111: QueryFactoryBase delegateFactory = null;
112: if (isCall(factory.getDelegateType())) {
113: delegateFactory = persistency.getCallFactory();
114: } else if (isQuery(factory.getDelegateType())) {
115: delegateFactory = persistency.getQueryFactory();
116: } else {
117: invalidateFactoryClass(factory.getDelegateType());
118: }
119: return delegateFactory;
120: }
121: }
122: }
|