001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.annotations.config.binding;
018:
019: import java.lang.reflect.ParameterizedType;
020: import java.lang.reflect.Type;
021: import java.util.Collection;
022:
023: import org.compass.annotations.Cascade;
024: import org.compass.annotations.ExcludeFromAll;
025: import org.compass.annotations.Index;
026: import org.compass.annotations.ManagedId;
027: import org.compass.annotations.ManagedIdIndex;
028: import org.compass.annotations.Reverse;
029: import org.compass.annotations.Store;
030: import org.compass.annotations.TermVector;
031: import org.compass.core.Property;
032: import org.compass.core.mapping.CascadeMapping;
033: import org.compass.core.mapping.ResourcePropertyMapping;
034: import org.compass.core.mapping.osem.ClassPropertyMapping;
035:
036: /**
037: * @author kimchy
038: */
039: public abstract class AnnotationsBindingUtils {
040:
041: public static String getCollectionParameterClassName(
042: Class<?> clazz, Type type) {
043: Class retVal = getCollectionParameterClass(clazz, type);
044: if (retVal == null) {
045: return null;
046: }
047: return retVal.getName();
048: }
049:
050: public static Class getCollectionParameterClass(Class<?> clazz,
051: Type type) {
052: if (Collection.class.isAssignableFrom(clazz)) {
053: if (type instanceof ParameterizedType) {
054: ParameterizedType paramType = (ParameterizedType) type;
055: Type[] actualTypeArguments = paramType
056: .getActualTypeArguments();
057: if (actualTypeArguments != null
058: && actualTypeArguments.length == 1) {
059: return (Class) actualTypeArguments[0];
060: }
061: }
062: }
063: return null;
064: }
065:
066: public static ClassPropertyMapping.ManagedId convert(
067: ManagedId managedId) throws IllegalArgumentException {
068: if (managedId == ManagedId.NA) {
069: return null;
070: } else if (managedId == ManagedId.AUTO) {
071: return ClassPropertyMapping.ManagedId.AUTO;
072: } else if (managedId == ManagedId.TRUE) {
073: return ClassPropertyMapping.ManagedId.TRUE;
074: } else if (managedId == ManagedId.FALSE) {
075: return ClassPropertyMapping.ManagedId.FALSE;
076: } else if (managedId == ManagedId.NO) {
077: return ClassPropertyMapping.ManagedId.NO;
078: } else if (managedId == ManagedId.NO_STORE) {
079: return ClassPropertyMapping.ManagedId.NO_STORE;
080: }
081: throw new IllegalArgumentException(
082: "Failed to convert managedId [" + managedId + "]");
083: }
084:
085: public static Property.TermVector convert(TermVector termVector)
086: throws IllegalArgumentException {
087: if (termVector == TermVector.NO) {
088: return Property.TermVector.NO;
089: } else if (termVector == TermVector.YES) {
090: return Property.TermVector.YES;
091: } else if (termVector == TermVector.WITH_POSITIONS) {
092: return Property.TermVector.WITH_POSITIONS;
093: } else if (termVector == TermVector.WITH_OFFSETS) {
094: return Property.TermVector.WITH_OFFSETS;
095: } else if (termVector == TermVector.WITH_POSITIONS_OFFSETS) {
096: return Property.TermVector.WITH_POSITIONS_OFFSETS;
097: }
098: throw new IllegalArgumentException(
099: "Failed to convert termVectory [" + termVector + "]");
100: }
101:
102: public static ResourcePropertyMapping.ReverseType convert(
103: Reverse reverse) throws IllegalArgumentException {
104: if (reverse == Reverse.NO) {
105: return ResourcePropertyMapping.ReverseType.NO;
106: } else if (reverse == Reverse.READER) {
107: return ResourcePropertyMapping.ReverseType.READER;
108: } else if (reverse == Reverse.STRING) {
109: return ResourcePropertyMapping.ReverseType.STRING;
110: }
111: throw new IllegalArgumentException(
112: "Failed to convert reverse [" + reverse + "]");
113: }
114:
115: public static ResourcePropertyMapping.ExcludeFromAllType convert(
116: ExcludeFromAll excludeFromAll)
117: throws IllegalArgumentException {
118: if (excludeFromAll == ExcludeFromAll.NO) {
119: return ResourcePropertyMapping.ExcludeFromAllType.NO;
120: } else if (excludeFromAll == ExcludeFromAll.NO_ANALYZED) {
121: return ResourcePropertyMapping.ExcludeFromAllType.NO_ANALYZED;
122: } else if (excludeFromAll == ExcludeFromAll.YES) {
123: return ResourcePropertyMapping.ExcludeFromAllType.YES;
124: }
125: throw new IllegalArgumentException(
126: "Failed to convert exclude from all [" + excludeFromAll
127: + "]");
128: }
129:
130: public static Property.Store convert(Store store)
131: throws IllegalArgumentException {
132: if (store == Store.NO) {
133: return Property.Store.NO;
134: } else if (store == Store.YES) {
135: return Property.Store.YES;
136: } else if (store == Store.COMPRESS) {
137: return Property.Store.COMPRESS;
138: }
139: throw new IllegalArgumentException("Failed to convert store ["
140: + store + "]");
141: }
142:
143: public static Property.Index convert(Index index)
144: throws IllegalArgumentException {
145: if (index == Index.NO) {
146: return Property.Index.NO;
147: } else if (index == Index.TOKENIZED) {
148: return Property.Index.TOKENIZED;
149: } else if (index == Index.UN_TOKENIZED) {
150: return Property.Index.UN_TOKENIZED;
151: }
152: throw new IllegalArgumentException("Failed to convert index ["
153: + index + "]");
154: }
155:
156: public static Property.Index convert(ManagedIdIndex index)
157: throws IllegalArgumentException {
158: if (index == ManagedIdIndex.NA) {
159: return null;
160: } else if (index == ManagedIdIndex.NO) {
161: return Property.Index.NO;
162: } else if (index == ManagedIdIndex.UN_TOKENIZED) {
163: return Property.Index.UN_TOKENIZED;
164: }
165: throw new IllegalArgumentException("Failed to convert index ["
166: + index + "]");
167: }
168:
169: public static CascadeMapping.Cascade convert(Cascade cascade)
170: throws IllegalArgumentException {
171: if (cascade == Cascade.ALL) {
172: return CascadeMapping.Cascade.ALL;
173: } else if (cascade == Cascade.CREATE) {
174: return CascadeMapping.Cascade.CREATE;
175: } else if (cascade == Cascade.DELETE) {
176: return CascadeMapping.Cascade.DELETE;
177: } else if (cascade == Cascade.SAVE) {
178: return CascadeMapping.Cascade.SAVE;
179: }
180: throw new IllegalArgumentException(
181: "Failed to convert cascade [" + cascade + "]");
182: }
183: }
|