001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.meta;
020:
021: import org.apache.commons.collections.bidimap.TreeBidiMap;
022: import org.apache.openjpa.conf.OpenJPAConfiguration;
023: import org.apache.openjpa.lib.util.Localizer;
024: import org.apache.openjpa.lib.util.StringDistance;
025: import org.apache.openjpa.util.MetaDataException;
026: import serp.util.Numbers;
027:
028: /**
029: * Strategies for persistent value generation.
030: *
031: * @author Abe White
032: * @since 0.4.0
033: */
034: public class ValueStrategies {
035:
036: /**
037: * No value strategy.
038: */
039: public static final int NONE = 0;
040:
041: /**
042: * "native" value strategy.
043: */
044: public static final int NATIVE = 1;
045:
046: /**
047: * "sequence" value strategy.
048: */
049: public static final int SEQUENCE = 2;
050:
051: /**
052: * "autoassigned" value strategy.
053: */
054: public static final int AUTOASSIGN = 3;
055:
056: /**
057: * "increment" value strategy.
058: */
059: public static final int INCREMENT = 4;
060:
061: /**
062: * "uuid-string" value strategy.
063: */
064: public static final int UUID_STRING = 5;
065:
066: /**
067: * "uuid-hex" value strategy.
068: */
069: public static final int UUID_HEX = 6;
070:
071: private static final Localizer _loc = Localizer
072: .forPackage(ValueStrategies.class);
073:
074: // table of names and strategies
075: private static final TreeBidiMap _map = new TreeBidiMap();
076:
077: static {
078: _map.put("none", Numbers.valueOf(NONE));
079: _map.put("native", Numbers.valueOf(NATIVE));
080: _map.put("sequence", Numbers.valueOf(SEQUENCE));
081: _map.put("autoassign", Numbers.valueOf(AUTOASSIGN));
082: _map.put("increment", Numbers.valueOf(INCREMENT));
083: _map.put("uuid-string", Numbers.valueOf(UUID_STRING));
084: _map.put("uuid-hex", Numbers.valueOf(UUID_HEX));
085: }
086:
087: /**
088: * Convert the given strategy to a name.
089: */
090: public static String getName(int strategy) {
091: Object code = Numbers.valueOf(strategy);
092: String name = (String) _map.getKey(code);
093: if (name != null)
094: return name;
095: throw new IllegalArgumentException(code.toString());
096: }
097:
098: /**
099: * Convert the given strategy name to its constant.
100: */
101: public static int getCode(String val, Object context) {
102: if (val == null)
103: return NONE;
104: Object code = _map.get(val);
105: if (code != null)
106: return ((Number) code).intValue();
107:
108: // not a recognized strategy; check for typo
109: String closest = StringDistance.getClosestLevenshteinDistance(
110: val, _map.keySet(), .5F);
111: String msg;
112: if (closest != null)
113: msg = _loc
114: .get(
115: "bad-value-strategy-hint",
116: new Object[] { context, val, closest,
117: _map.keySet() }).getMessage();
118: else
119: msg = _loc.get("bad-value-strategy", context, val,
120: _map.keySet()).getMessage();
121: throw new IllegalArgumentException(msg);
122: }
123:
124: /**
125: * Assert that the given strategy is supported by the current runtime.
126: */
127: public static void assertSupported(int strategy,
128: MetaDataContext context, String attributeName) {
129: OpenJPAConfiguration conf = context.getRepository()
130: .getConfiguration();
131: boolean supported = true;
132: switch (strategy) {
133: case AUTOASSIGN:
134: supported = conf.supportedOptions().contains(
135: OpenJPAConfiguration.OPTION_VALUE_AUTOASSIGN);
136: break;
137: case INCREMENT:
138: supported = conf.supportedOptions().contains(
139: OpenJPAConfiguration.OPTION_VALUE_INCREMENT);
140: break;
141: case NATIVE:
142: supported = context instanceof ClassMetaData;
143: break;
144: }
145: if (!supported)
146: throw new MetaDataException(_loc.get(
147: "unsupported-value-strategy", context,
148: getName(strategy), attributeName));
149: }
150: }
|