001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.driver.services.portal;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: /**
029: */
030: public class RenderConfig {
031: private static final Log LOG = LogFactory
032: .getLog(RenderConfig.class);
033:
034: private Map pages;
035: private String defaultPageId;
036:
037: // internally used.
038: private int orderNumberCounter = 0;
039: private Comparator pageComparator;
040:
041: public RenderConfig() {
042: this .pages = new java.util.HashMap();
043: this .pageComparator = new Comparator() {
044: public int compare(Object a, Object b) {
045: PageConfig pa = (PageConfig) a;
046: PageConfig pb = (PageConfig) b;
047: if (pa.getOrderNumber() > pb.getOrderNumber()) {
048: return 1;
049: } else if (pa.getOrderNumber() == pb.getOrderNumber()) {
050: return 0;
051: } else {
052: return -1;
053: }
054: }
055:
056: public boolean equals(Object a) {
057: return false;
058: }
059: };
060: }
061:
062: public String getDefaultPageId() {
063: return defaultPageId;
064: }
065:
066: public void setDefaultPageId(String defaultPageId) {
067: this .defaultPageId = defaultPageId;
068: }
069:
070: public List getPages() {
071: List col = new ArrayList(pages.values());
072: Collections.sort(col, pageComparator);
073: return col;
074: }
075:
076: public PageConfig getPageConfig(String pageId) {
077: if (pageId == null || "".equals(pageId)) {
078: if (LOG.isDebugEnabled()) {
079: LOG
080: .debug("Requested page is null. Returning default: "
081: + defaultPageId);
082: }
083: pageId = defaultPageId;
084: }
085: // return (PageConfig) pages.get(pageId);
086:
087: // TODO: Make sure this is needed.
088: //This is the PLUTO-251 fix submitted by Charles Severence. Thank you!!!
089: // Sometimes pages come with a prefix of a slash - if the page is not
090: // found, and the first character of the pageId is a slash we attempt
091: // to look up the page without the slash.
092:
093: PageConfig retval = (PageConfig) pages.get(pageId);
094:
095: if (retval == null && pageId.startsWith("/")
096: && pageId.length() > 2) {
097: retval = (PageConfig) pages.get(pageId.substring(1));
098: }
099:
100: if (retval == null) {
101: LOG.warn("Couldn't find a PageConfig for page ID: ["
102: + pageId + "]");
103: if ((retval = (PageConfig) pages.get(defaultPageId)) != null) {
104: if (LOG.isDebugEnabled()) {
105: LOG.debug("Returning default page ID: ["
106: + defaultPageId + "]");
107: }
108: } else {
109: LOG
110: .error("Could not find default page Id for render config!");
111: }
112: }
113: return retval;
114: }
115:
116: public void addPage(PageConfig config) {
117: config.setOrderNumber(orderNumberCounter++);
118: pages.put(config.getName(), config);
119: }
120:
121: public void removePage(PageConfig config) {
122: pages.remove(config.getName());
123: }
124:
125: }
|