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.StringTokenizer;
009:
010: import org.jasig.portal.PortalException;
011: import org.jasig.portal.layout.alm.ALNode;
012: import org.jasig.portal.layout.node.ILayoutNode;
013: import org.jasig.portal.utils.CommonUtils;
014:
015: /**
016: * PriorityRestriction checks the priority restriction for a given ALNode object.
017: *
018: * Prior to uPortal 2.5, this class existed in the package org.jasig.portal.layout.restrictions.
019: * It was moved to its present package to reflect that it is part of Aggregated Layouts.
020: *
021: * @author <a href="mailto:mvi@immagic.com">Michael Ivanov</a>
022: * @version $Revision: 35731 $
023: */
024: public class PriorityRestriction extends ALRestriction {
025:
026: private int minPriority, maxPriority;
027:
028: public PriorityRestriction(String name, String nodePath) {
029: super (name, nodePath);
030: }
031:
032: public PriorityRestriction(String name) {
033: super (name);
034: }
035:
036: public PriorityRestriction() {
037: super ();
038: }
039:
040: /**
041: * Returns the maximum value of the given restriction
042: * @return a maxPriority
043: */
044: public int getMaxValue() {
045: return maxPriority;
046: }
047:
048: /**
049: * Returns the minimum value of the given restriction
050: * @return a minPriority
051: */
052: public int getMinValue() {
053: return minPriority;
054: }
055:
056: /**
057: * Returns the minimum and maximum values of the given restriction as an integer array
058: * @return an integer array
059: */
060: public int[] getRange() {
061: return new int[] { minPriority, maxPriority };
062: }
063:
064: /**
065: * Parses the restriction expression of the current node
066: * @exception PortalException
067: */
068: protected void parseRestrictionExpression() throws PortalException {
069: try {
070: String restrictionExp = getRestrictionExpression();
071: if (restrictionExp.indexOf('-') > 0) {
072: StringTokenizer st = new StringTokenizer(
073: restrictionExp, "-");
074: String min = st.nextToken();
075: String max = st.nextToken();
076: minPriority = CommonUtils.parseInt(min, 0);
077: maxPriority = CommonUtils.parseInt(max,
078: Integer.MAX_VALUE);
079: } else {
080: minPriority = CommonUtils.parseInt(restrictionExp, 0);
081: maxPriority = minPriority;
082: }
083: } catch (Exception e) {
084: throw new PortalException(e);
085: }
086: }
087:
088: /**
089: * Checks the restriction for the specified node
090: * @param propertyValue a <code>String</code> property value to be checked
091: * @exception PortalException
092: */
093: public boolean checkRestriction(String propertyValue)
094: throws PortalException {
095: int priority = CommonUtils.parseInt(propertyValue, 0);
096: if (priority <= maxPriority && priority >= minPriority)
097: return true;
098: return false;
099: }
100:
101: /**
102: * Checks the restriction for the current node
103: * @exception PortalException
104: */
105: public boolean checkRestriction(ILayoutNode node)
106: throws PortalException {
107:
108: if (!(node instanceof ALNode))
109: throw new PortalException("The node must be ALNode type!");
110: ALNode alNode = (ALNode) node;
111:
112: // if we have a related priority restriction we should check the priority ranges
113: if (nodePath != null
114: && !nodePath.equals(LOCAL_RESTRICTION_PATH)) {
115: PriorityRestriction restriction = (PriorityRestriction) alNode
116: .getRestriction(getUniqueKey(RestrictionTypes.PRIORITY_RESTRICTION));
117: if (restriction != null) {
118: int[] range = restriction.getRange();
119: if (minPriority > range[1] || maxPriority < range[0])
120: return false;
121: }
122: return true;
123: }
124:
125: int priority = alNode.getPriority();
126: if (priority <= maxPriority && priority >= minPriority)
127: return true;
128: return false;
129: }
130:
131: public void setRestriction(int minPriority, int maxPriority) {
132: this .minPriority = minPriority;
133: this .maxPriority = maxPriority;
134: setRestrictionExpression(new String(minPriority + "-"
135: + maxPriority));
136: }
137:
138: }
|