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.core.config.process;
018:
019: import java.util.Iterator;
020:
021: import org.compass.core.config.CompassSettings;
022: import org.compass.core.converter.ConverterLookup;
023: import org.compass.core.engine.naming.PropertyNamingStrategy;
024: import org.compass.core.mapping.CompassMapping;
025: import org.compass.core.mapping.InvalidMappingException;
026: import org.compass.core.mapping.Mapping;
027: import org.compass.core.mapping.MappingException;
028: import org.compass.core.mapping.ResourceMapping;
029: import org.compass.core.mapping.ResourcePropertyMapping;
030: import org.compass.core.mapping.osem.ClassMapping;
031: import org.compass.core.mapping.osem.HasRefAliasMapping;
032:
033: /**
034: * @author kimchy
035: */
036: public class ValidatorMappingProcessor implements MappingProcessor {
037:
038: public CompassMapping process(CompassMapping compassMapping,
039: PropertyNamingStrategy namingStrategy,
040: ConverterLookup converterLookup, CompassSettings settings)
041: throws MappingException {
042: ResourceMapping[] rootMappings = compassMapping
043: .getRootMappings();
044: for (int i = 0; i < rootMappings.length; i++) {
045: ResourceMapping resourceMapping = rootMappings[i];
046: validateRootMapping(resourceMapping);
047: }
048: return compassMapping;
049: }
050:
051: private void validateRootMapping(ResourceMapping resourceMapping)
052: throws MappingException {
053: validatieHasAtLeastOneId(resourceMapping);
054: validateMulitRefAliasHasPoly(resourceMapping);
055: String[] resourcePropertyNames = resourceMapping
056: .getResourcePropertyNames();
057: for (int i = 0; i < resourcePropertyNames.length; i++) {
058: String propertyName = resourcePropertyNames[i];
059: ResourcePropertyMapping[] resourcePropertyMapping = resourceMapping
060: .getResourcePropertyMappings(propertyName);
061: validateDuplicateExcludeFromAll(resourceMapping,
062: propertyName, resourcePropertyMapping);
063: validateDuplicateAnalyzer(resourceMapping, propertyName,
064: resourcePropertyMapping);
065: }
066: }
067:
068: private void validatieHasAtLeastOneId(
069: ResourceMapping resourceMapping) {
070: Mapping[] idMappings = resourceMapping.getIdMappings();
071: if (idMappings.length == 0) {
072: throw new MappingException(
073: "Mapping for alias ["
074: + resourceMapping.getAlias()
075: + "] has no id mappings defined. "
076: + "Either you forgot to add id mappings for it, or it is a component mapping that requires no ids and it "
077: + "is not configured with root=false");
078: }
079: }
080:
081: private void validateMulitRefAliasHasPoly(
082: ResourceMapping resourceMapping) {
083: if (!(resourceMapping instanceof ClassMapping)) {
084: return;
085: }
086: ClassMapping classMapping = (ClassMapping) resourceMapping;
087: for (Iterator it = classMapping.mappingsIt(); it.hasNext();) {
088: Mapping innerMapping = (Mapping) it.next();
089: if (innerMapping instanceof HasRefAliasMapping) {
090: ClassMapping[] refMappings = ((HasRefAliasMapping) innerMapping)
091: .getRefClassMappings();
092: if (refMappings.length > 1) {
093: for (int i = 0; i < refMappings.length; i++) {
094: if (!refMappings[i].isPoly()) {
095: throw new MappingException(
096: "Mapping for alias ["
097: + classMapping.getAlias()
098: + "] and reference/component mapping ["
099: + innerMapping.getName()
100: + "] has more than one ref-alias mappings, but class mapping ["
101: + refMappings[i].getAlias()
102: + "] is not defined as poly");
103: }
104: }
105: }
106: }
107: }
108:
109: }
110:
111: private void validateDuplicateExcludeFromAll(
112: ResourceMapping resourceMapping, String propertyName,
113: ResourcePropertyMapping[] resourcePropertyMapping)
114: throws MappingException {
115: if (resourcePropertyMapping.length == 1) {
116: return;
117: }
118: ResourcePropertyMapping.ExcludeFromAllType excludeFromAll = resourcePropertyMapping[0]
119: .getExcludeFromAll();
120: for (int i = 1; i < resourcePropertyMapping.length; i++) {
121: if (resourcePropertyMapping[i].isInternal()) {
122: continue;
123: }
124: if (excludeFromAll != resourcePropertyMapping[i]
125: .getExcludeFromAll()) {
126: throw new InvalidMappingException(
127: "Resource property / meta-data ["
128: + propertyName
129: + "] of alias ["
130: + resourceMapping.getAlias()
131: + "] has different exclude from all settings");
132: }
133: }
134: }
135:
136: private void validateDuplicateAnalyzer(
137: ResourceMapping resourceMapping, String propertyName,
138: ResourcePropertyMapping[] resourcePropertyMapping)
139: throws MappingException {
140: if (resourcePropertyMapping.length == 1) {
141: return;
142: }
143: boolean first = true;
144: String lastAnalyzer = null;
145: for (int i = 0; i < resourcePropertyMapping.length; i++) {
146: ResourcePropertyMapping propertyMapping = resourcePropertyMapping[i];
147: // don't worry about internal properties, since they have nothing to
148: // do with the analyzer
149: if (propertyMapping.isInternal()) {
150: continue;
151: }
152: if (propertyMapping.getAnalyzer() != null) {
153: if (lastAnalyzer == null && !first) {
154: // we passed several mappings with no analyzers, and now we
155: // found one that has
156: throw new InvalidMappingException(
157: "Resource property / meta-data ["
158: + propertyName
159: + "] of alias ["
160: + resourceMapping.getAlias()
161: + "] has an anlyzer set, and some do not. Please set for all of them the same analyzer.");
162: }
163: if (first) {
164: lastAnalyzer = propertyMapping.getAnalyzer();
165: } else {
166: if (!propertyMapping.getAnalyzer().equals(
167: lastAnalyzer)) {
168: throw new InvalidMappingException(
169: "Resource property / meta-data ["
170: + propertyName
171: + "] of alias ["
172: + resourceMapping.getAlias()
173: + "] has several anlyzers set. Please set for all of them the same analyzer.");
174: }
175: }
176: } else {
177: if (lastAnalyzer != null) {
178: throw new InvalidMappingException(
179: "Resource property / meta-data ["
180: + propertyName
181: + "] of alias ["
182: + resourceMapping.getAlias()
183: + "] has several anlyzers set. Please set for all of them the same analyzer.");
184: }
185: }
186: if (first) {
187: first = false;
188: }
189: }
190: }
191:
192: }
|