001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.layout.restrictions.alm;
007:
008: import java.util.List;
009: import java.util.StringTokenizer;
010: import java.util.Vector;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.jasig.portal.PortalException;
015: import org.jasig.portal.utils.CommonUtils;
016:
017: /**
018: * DepthRestriction checks the priority restriction for a given UserLayoutNode object.
019: * <p>
020: * Company: Instructional Media & Magic
021: *
022: * Prior to uPortal 2.5, this class existed in the package org.jasig.portal.layout.restrictions.
023: * It was moved to its present package to reflect that it is part of Aggregated Layouts.
024: *
025: * @author <a href="mailto:mvi@immagic.com">Michael Ivanov</a>
026: * @version $Revision: 36068 $
027: */
028: public class DepthRestriction extends ALRestriction {
029: private static final Log LOG = LogFactory
030: .getLog(ALRestriction.class);
031:
032: private Integer[] minDepthArray, maxDepthArray, depthArray;
033:
034: public DepthRestriction(String name, String nodePath) {
035: super (name, nodePath);
036: }
037:
038: public DepthRestriction(String name) {
039: super (name);
040: }
041:
042: public DepthRestriction() {
043: super ();
044: }
045:
046: /**
047: * Parses the restriction expression of the current node
048: * @exception PortalException
049: */
050: protected void parseRestrictionExpression() throws PortalException {
051: try {
052: String restrictionExp = getRestrictionExpression();
053: List minDepthList = new Vector();
054: List maxDepthList = new Vector();
055: List depthList = new Vector();
056: StringTokenizer st = new StringTokenizer(restrictionExp,
057: ",");
058: while (st.hasMoreTokens()) {
059: String token = st.nextToken();
060: int index = token.indexOf('-');
061: if (index > 0) {
062: minDepthList.add(token.substring(0, index));
063: maxDepthList.add(token.substring(index + 1));
064: } else
065: depthList.add(token);
066: }
067: int size = minDepthList.size();
068: minDepthArray = new Integer[size];
069: maxDepthArray = new Integer[size];
070: for (int i = 0; i < size; i++) {
071: minDepthArray[i] = Integer
072: .valueOf((String) minDepthList.get(i));
073: maxDepthArray[i] = Integer
074: .valueOf((String) maxDepthList.get(i));
075: }
076: size = depthList.size();
077: depthArray = new Integer[size];
078: for (int i = 0; i < size; i++)
079: depthArray[i] = Integer.valueOf((String) depthList
080: .get(i));
081:
082: } catch (Exception e) {
083: LOG.error(e, e);
084: throw new PortalException(e);
085: }
086:
087: }
088:
089: /**
090: * Checks the restriction for the specified node
091: * @param propertyValue a <code>String</code> property value to be checked
092: * @exception PortalException
093: */
094: public boolean checkRestriction(String propertyValue)
095: throws PortalException {
096: int depth = CommonUtils.parseInt(propertyValue);
097: for (int i = 0; i < minDepthArray.length
098: || i < depthArray.length; i++) {
099: if (i < minDepthArray.length)
100: if (depth <= maxDepthArray[i].intValue()
101: && depth >= minDepthArray[i].intValue())
102: return true;
103: if (i < depthArray.length)
104: if (depthArray[i].intValue() == depth)
105: return true;
106: }
107: return false;
108: }
109:
110: }
|