View Javadoc
1   /*******************************************************************************
2    * Copyright 2012 Internet2
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  /*
17  Copyright 2004-2006 University Corporation for Advanced Internet Development, Inc.
18  Copyright 2004-2006 The University Of Bristol
19  
20  Licensed under the Apache License, Version 2.0 (the "License");
21  you may not use this file except in compliance with the License.
22  You may obtain a copy of the License at
23  
24    http://www.apache.org/licenses/LICENSE-2.0
25  
26  Unless required by applicable law or agreed to in writing, software
27  distributed under the License is distributed on an "AS IS" BASIS,
28  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  See the License for the specific language governing permissions and
30  limitations under the License.
31  */
32  package edu.internet2.middleware.grouper.ui;
33  
34  import java.util.ResourceBundle;
35  
36  import edu.internet2.middleware.grouper.Membership;
37  import edu.internet2.middleware.grouper.Stem;
38  import edu.internet2.middleware.grouper.ui.util.MembershipAsMap;
39  import edu.internet2.middleware.grouper.ui.util.StemAsMap;
40  
41  /**
42   * Implementation of ComparatorHelper used to sort Stems. The context affects sorting thus:
43   * <ul>
44   * <li>If it starts 'search:', then anything following : is used as the sort field </li>
45   * <li>Otherwise 'stem.sort.&lt;context&gt;', 'stem.sort.&lt;default&gt;' and 'stem.display' are looked up until a value is found</li>
46   * </ul>
47   * If the sort field contains spaces, the field is 'split' and each sub part is assumed to be separate attribute to sort on.
48   *
49   * <p />
50   * 
51   * @author Gary Brown.
52   * @version $Id: StemComparatorHelper.java,v 1.2 2008-03-03 13:58:25 isgwb Exp $
53   */
54  
55  public class StemComparatorHelper implements GrouperComparatorHelper{
56  
57  	/**
58  	 * 
59  	 */
60  	public StemComparatorHelper() {
61  		super();
62  		// TODO Auto-generated constructor stub
63  	}
64  	
65  
66  	/**
67  	 * 
68  	 * @see edu.internet2.middleware.grouper.ui.GrouperComparatorHelper#getComparisonString(java.lang.Object, java.util.ResourceBundle, java.lang.String)
69  	 */
70  	public String getComparisonString(Object obj, ResourceBundle config,
71  			String context) {
72  		Stem stem = null;
73  		if(obj instanceof Stem) {
74  			stem=(Stem)obj;
75  		}else if(obj instanceof StemAsMap) {
76  			stem = (Stem)((StemAsMap)obj).getWrappedObject();
77  		}else if(obj instanceof Membership) {
78  			stem = ((Membership)obj).getStem();
79  		}else if(obj instanceof MembershipAsMap) {
80  			stem = ((Membership)((MembershipAsMap)obj).getWrappedObject()).getStem();
81  		
82  		}else{
83  			throw new IllegalArgumentException(obj + " is not a Stem");
84  		}
85  		
86  		String attrStr=null;
87  		if(context.startsWith("search:")) {
88  			attrStr=context.substring(7);
89  		}else{
90  			try {
91  				attrStr=config.getString("stem.sort." + context);
92  			}catch(Exception e){}
93  			if(attrStr==null) {
94  				try {
95  					attrStr=config.getString("stem.sort.default");
96  				}catch(Exception e){}
97  				
98  			}
99  			if(attrStr==null) {
100 				try {
101 					attrStr=config.getString("stem.display");
102 				}catch(Exception e){}
103 				
104 			}
105 		}
106 		String[] parts=attrStr.split(" ");
107 		StringBuffer sb=new StringBuffer();
108 		String val="";
109 		for(int i=0;i<parts.length;i++) {
110 			val="";
111 			try {
112 				val=getStemAttribute(stem,parts[i]);
113 			}catch(Exception e){}
114 			sb.append(val);
115 		}
116 		boolean sortLowercase = true;
117 		try {
118 			if("false".equals(config.getString("comparator.sort.lowercase"))) sortLowercase=false;
119 		}catch(Exception e) {}
120 		if(sortLowercase) {
121 			return sb.toString().toLowerCase();
122 		}
123 		return sb.toString();
124 	}
125 	
126 	/**
127 	 * 
128 	 * @param stem
129 	 * @param attr
130 	 * @return string
131 	 */
132 	private String getStemAttribute(Stem stem, String attr) {
133 		if("extension".equals(attr)) {
134 			return stem.getExtension();
135 		}else if("displayExtension".equals(attr)) {
136 			return stem.getDisplayExtension();
137 		}else if("name".equals(attr)) {
138 			return stem.getName();
139 		}else if("displayName".equals(attr)) {
140 			return stem.getDisplayName();
141 		}else if("deascription".equals(attr)) {
142 			return stem.getDescription();
143 		}
144 		return "";
145 		
146 	}
147 }