001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc.metadata;
023:
024: import org.w3c.dom.Element;
025: import org.jboss.metadata.MetaData;
026: import org.jboss.deployment.DeploymentException;
027:
028: import java.util.List;
029: import java.util.Iterator;
030: import java.util.Collections;
031: import java.util.ArrayList;
032:
033: /**
034: * Represents
035: * <left-join cmr-field="lineItems">
036: * <left-join cmr-field="product" eager-load-group="product"/>
037: * </left-join>
038: *
039: * @version <tt>$Revision: 57209 $</tt>
040: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
041: */
042: public final class JDBCLeftJoinMetaData {
043: private final String cmrField;
044: private final String eagerLoadGroup;
045: private final List leftJoinList;
046:
047: public static List readLeftJoinList(Iterator leftJoinIterator)
048: throws DeploymentException {
049: List leftJoinList;
050: if (leftJoinIterator.hasNext()) {
051: leftJoinList = new ArrayList();
052: while (leftJoinIterator.hasNext()) {
053: Element leftJoinElement = (Element) leftJoinIterator
054: .next();
055: JDBCLeftJoinMetaData leftJoin = new JDBCLeftJoinMetaData(
056: leftJoinElement);
057: leftJoinList.add(leftJoin);
058: }
059: } else {
060: leftJoinList = Collections.EMPTY_LIST;
061: }
062: return leftJoinList;
063: }
064:
065: /**
066: * Used only from the testsuite.
067: */
068: public JDBCLeftJoinMetaData(String cmrField, String eagerLoadGroup,
069: List leftJoinList) {
070: this .cmrField = cmrField;
071: this .eagerLoadGroup = eagerLoadGroup;
072: this .leftJoinList = leftJoinList;
073: }
074:
075: public JDBCLeftJoinMetaData(Element element)
076: throws DeploymentException {
077: cmrField = element.getAttribute("cmr-field");
078: if (cmrField == null || cmrField.trim().length() == 0) {
079: throw new DeploymentException(
080: "left-join MUST have non-empty cmr-field attribute.");
081: }
082:
083: String eagerLoadGroup = element
084: .getAttribute("eager-load-group");
085: if (eagerLoadGroup == null
086: || eagerLoadGroup.trim().length() == 0) {
087: this .eagerLoadGroup = "*";
088: } else {
089: this .eagerLoadGroup = eagerLoadGroup;
090: }
091:
092: Iterator leftJoinIterator = MetaData.getChildrenByTagName(
093: element, "left-join");
094: leftJoinList = readLeftJoinList(leftJoinIterator);
095: }
096:
097: public String getCmrField() {
098: return cmrField;
099: }
100:
101: public String getEagerLoadGroup() {
102: return eagerLoadGroup;
103: }
104:
105: public Iterator getLeftJoins() {
106: return leftJoinList.iterator();
107: }
108:
109: public boolean equals(Object o) {
110: boolean result;
111: if (o == this ) {
112: result = true;
113: } else if (o instanceof JDBCLeftJoinMetaData) {
114: JDBCLeftJoinMetaData other = (JDBCLeftJoinMetaData) o;
115: result = (cmrField == null ? other.cmrField == null
116: : cmrField.equals(other.cmrField))
117: && (eagerLoadGroup == null ? other.eagerLoadGroup == null
118: : eagerLoadGroup
119: .equals(other.eagerLoadGroup))
120: && (leftJoinList == null ? other.leftJoinList == null
121: : leftJoinList.equals(other.leftJoinList));
122: } else {
123: result = false;
124: }
125: return result;
126: }
127:
128: public int hashCode() {
129: int result = Integer.MIN_VALUE;
130: result += (cmrField == null ? 0 : cmrField.hashCode());
131: result += (eagerLoadGroup == null ? 0 : eagerLoadGroup
132: .hashCode());
133: result += (leftJoinList == null ? 0 : leftJoinList.hashCode());
134: return result;
135: }
136:
137: public String toString() {
138: return "[cmr-field=" + cmrField + ", eager-load-group="
139: + eagerLoadGroup + ", left-join=" + leftJoinList + ']';
140: }
141: }
|