001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/common/tool-lib/src/java/org/theospi/portfolio/list/tool/ListTool.java $
003: * $Id: ListTool.java 22946 2007-03-19 17:37:01Z john.ellis@rsmart.com $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.list.tool;
021:
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.component.cover.ServerConfigurationService;
031: import org.sakaiproject.tool.api.ToolSession;
032: import org.sakaiproject.tool.cover.SessionManager;
033: import org.theospi.portfolio.list.intf.DecoratedListItem;
034: import org.theospi.portfolio.list.intf.ListItemUtils;
035: import org.theospi.portfolio.list.intf.ListService;
036: import org.theospi.portfolio.list.model.Column;
037: import org.theospi.portfolio.list.model.ListConfig;
038: import org.theospi.portfolio.shared.tool.ToolBase;
039:
040: public class ListTool extends ToolBase implements ListItemUtils {
041: protected final transient Log logger = LogFactory
042: .getLog(getClass());
043:
044: private ListService listService;
045: private DecoratedEntry currentEntry;
046:
047: private ListConfig currentConfig;
048: private String sortCol;
049: private int sortDir = SORT_ASC;
050:
051: public static final int SORT_ASC = 1;
052: public static final int SORT_DESC = -1;
053: public static final String SORT_FIELD = "org.theospi.portfolio.list.sortField";
054: public static final String SORT_DIR = "org.theospi.portfolio.list.sortDir";
055: public static final int TOTAL_COLUMNS = 10;
056:
057: public ListTool() {
058: logger.debug("ListTool()");
059: }
060:
061: public String formatMessage(String key, Object[] args) {
062: return getMessageFromBundle(key, args);
063: }
064:
065: public List getEntries() {
066: List entries = getListService().getList();
067: List returned = new ArrayList();
068:
069: int count = 0;
070: for (Iterator i = entries.iterator(); i.hasNext();) {
071: Object listItem = i.next();
072: if (listItem instanceof DecoratedListItem) {
073: ((DecoratedListItem) listItem).setListItemUtils(this );
074: }
075: returned.add(new DecoratedEntry(listItem, getListService(),
076: this ));
077: count++;
078: if (getCurrentConfig().getRows() > 0
079: && count == getCurrentConfig().getRows()) {
080: //sort(returned);
081: break;
082: //return returned;
083: }
084: }
085:
086: sort(returned);
087: return returned;
088: }
089:
090: protected void sort(List list) {
091: ToolSession session = SessionManager.getCurrentToolSession();
092: String sortField = getDefaultSortColumn();
093: int sortDir = SORT_ASC;
094: try {
095: if (session.getAttribute(SORT_FIELD) != null)
096: sortField = (String) session.getAttribute(SORT_FIELD);
097:
098: if (session.getAttribute(SORT_DIR) != null)
099: sortDir = ((Integer) session.getAttribute(SORT_DIR))
100: .intValue();
101: } catch (Exception e) {
102: logger
103: .debug("Exception getting sorting details. Use the defaults instead.");
104: }
105:
106: if (sortField != null && !sortField.equals("")) {
107: Collections.sort(list, new DecoratedEntryComparator(
108: sortField, sortDir));
109: }
110: }
111:
112: public boolean isCurrentSortField(String field) {
113: ToolSession session = SessionManager.getCurrentToolSession();
114: String sortField = getDefaultSortColumn();
115: if (session.getAttribute(SORT_FIELD) != null)
116: sortField = (String) session.getAttribute(SORT_FIELD);
117:
118: return field.equals(sortField);
119: }
120:
121: public boolean lookUpInBundle(String field) {
122: return getBundleLookupColumns().contains(field);
123: }
124:
125: public int getCurrentSortDir() {
126: ToolSession session = SessionManager.getCurrentToolSession();
127: int sortDir = SORT_ASC;
128: try {
129: sortDir = ((Integer) session.getAttribute(SORT_DIR))
130: .intValue();
131: } catch (Exception e) {
132: logger
133: .debug("Exception getting sorting details. Use the defaults instead.");
134: }
135: return sortDir;
136: }
137:
138: public List getDisplayColumns() {
139: List columns = getListService().getCurrentDisplayColumns();
140: List decoratedColumns = new ArrayList(columns.size());
141: for (Iterator i = columns.iterator(); i.hasNext();) {
142: Column column = (Column) i.next();
143: decoratedColumns.add(new DecoratedColumn(column, this ));
144: }
145: return decoratedColumns;
146: }
147:
148: public String getServerUrl() {
149: return ServerConfigurationService.getServerUrl();
150: }
151:
152: public List getSelectedColumns() {
153: Map selected = getCurrentConfig().getSelected();
154: List decoratedColumns = new ArrayList(TOTAL_COLUMNS);
155: for (int i = 0; i < TOTAL_COLUMNS; i++) {
156: Column column = (Column) selected.get(i);
157: decoratedColumns.add(new DecoratedColumn(column, this ));
158: }
159: return decoratedColumns;
160: }
161:
162: public List getSortableColumns() {
163: return getListService().getSortableColumns();
164: }
165:
166: public List getBundleLookupColumns() {
167: return getListService().getBundleLookupColumns();
168: }
169:
170: private String getDefaultSortColumn() {
171: return getListService().getDefaultSortColumn();
172: }
173:
174: public ListService getListService() {
175: return listService;
176: }
177:
178: public void setListService(ListService listService) {
179: this .listService = listService;
180: setCurrentConfig(getListService().getCurrentConfig());
181: }
182:
183: public String processActionOptions() {
184: setCurrentConfig(getListService().getCurrentConfig());
185:
186: return "options";
187: }
188:
189: public String processActionSort(DecoratedColumn column, int dir) {
190: ToolSession session = SessionManager.getCurrentToolSession();
191: session.setAttribute(SORT_FIELD, column.getBase().getName());
192: session.setAttribute(SORT_DIR, dir);
193:
194: return "main";
195: }
196:
197: public String processMain() {
198: return "main";
199: }
200:
201: public String processActionOptionsSave() {
202: getListService().saveOptions(getCurrentConfig());
203: return "main";
204: }
205:
206: public DecoratedEntry getCurrentEntry() {
207: return currentEntry;
208: }
209:
210: public void setCurrentEntry(DecoratedEntry currentEntry) {
211: this .currentEntry = currentEntry;
212: }
213:
214: public ListConfig getCurrentConfig() {
215: return currentConfig;
216: }
217:
218: public void setCurrentConfig(ListConfig currentConfig) {
219: this .currentConfig = currentConfig;
220: }
221:
222: /**
223: * @return the sortCol
224: */
225: public String getSortCol() {
226: return sortCol;
227: }
228:
229: /**
230: * @param sortCol the sortCol to set
231: */
232: public void setSortCol(String sortCol) {
233: this .sortCol = sortCol;
234: }
235:
236: /**
237: * @return the sortDir
238: */
239: public int getSortDir() {
240: return sortDir;
241: }
242:
243: /**
244: * @param sortDir the sortDir to set
245: */
246: public void setSortDir(int sortDir) {
247: this.sortDir = sortDir;
248: }
249:
250: }
|