001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Iterator;
027:
028: import com.methodhead.persistable.Persistable;
029: import com.methodhead.persistable.PersistableException;
030: import com.methodhead.aikp.IntKey;
031: import com.methodhead.tree.Tree;
032:
033: import org.apache.commons.beanutils.DynaClass;
034: import org.apache.commons.beanutils.DynaProperty;
035: import org.apache.commons.beanutils.BasicDynaClass;
036:
037: import com.methodhead.sitecontext.SiteContext;
038: import com.methodhead.sitecontext.SiteContextCapable;
039: import java.util.Enumeration;
040:
041: /**
042: * A hierarchy of links to pages.
043: */
044: public class SiteMap extends Tree implements SiteContextCapable {
045:
046: private static DynaClass dynaClass_ = null;
047:
048: static {
049: DynaProperty[] dynaProperties = new DynaProperty[] {
050: new DynaProperty("sitecontext_id", Integer.class),
051: new DynaProperty("page_id", Integer.class),
052: new DynaProperty("parent_id", Integer.class),
053: new DynaProperty("rank", Integer.class) };
054:
055: dynaClass_ = new BasicDynaClass("shim_link", Persistable.class,
056: dynaProperties);
057: }
058:
059: // constructors /////////////////////////////////////////////////////////////
060:
061: // constants ////////////////////////////////////////////////////////////////
062:
063: // classes //////////////////////////////////////////////////////////////////
064:
065: // methods //////////////////////////////////////////////////////////////////
066:
067: /**
068: * Returns the current site context for this object, throwing an exception if
069: * it hasn't been set. NOT UNIT TESTED.
070: */
071: public SiteContext getSiteContext() {
072: if (siteContext_ == null)
073: throw new ShimException("Site context has not been set.");
074:
075: return siteContext_;
076: }
077:
078: /**
079: * Implements <tt>SiteContextCapable.setSiteContext()</tt>. NOT UNIT TESTED.
080: */
081: public void setSiteContext(SiteContext siteContext) {
082: siteContext_ = siteContext;
083: }
084:
085: /**
086: * Returns the link with <tt>pageId</tt>, or <tt>null</tt> if no such link
087: * can be found.
088: */
089: public Link find(
090: int pageId ) {
091:
092: if ( root_ == null )
093: return null;
094:
095: Enumeration enum = root_.depthFirstEnumeration();
096: while ( enum.hasMoreElements() ) {
097: Link link = ( Link )enum.nextElement();
098: if ( link.getPageId() == pageId )
099: return link;
100: }
101:
102: return null;
103: }
104:
105: /**
106: * Saves the site map to the database.
107: */
108: public void save() {
109: Persistable.deleteAll(
110: dynaClass_, "sitecontext_id=" + getSiteContext().getInt( "id" ) );
111:
112: Persistable p = new Persistable( dynaClass_ );
113: p.setInt( "sitecontext_id", getSiteContext().getInt( "id" ) );
114:
115: if ( root_ != null ) {
116: Enumeration enum = root_.depthFirstEnumeration();
117: while ( enum.hasMoreElements() ) {
118: Link link = ( Link )enum.nextElement();
119: Link parent = ( Link )link.getParent();
120:
121: if ( parent == null ) {
122: p.setInt( "page_id", link.getPageId() );
123: p.setInt( "parent_id", 0 );
124: p.setInt( "rank", 0 );
125: }
126: else {
127: p.setInt( "page_id", link.getPageId() );
128: p.setInt( "parent_id", parent.getPageId() );
129: p.setInt( "rank", parent.getIndex( link ) );
130: }
131:
132: p.saveNew();
133: }
134: }
135: }
136:
137: /**
138: * Loads the site map.
139: */
140: public void load() {
141: List l = Persistable.loadAll(dynaClass_, "sitecontext_id="
142: + getSiteContext().getInt("id"), "parent_id, rank");
143:
144: //
145: // build a map of links
146: //
147: Map linkMap = new HashMap();
148: Page page = new Page();
149: page.setSiteContext(getSiteContext());
150:
151: for (Iterator iter = l.iterator(); iter.hasNext();) {
152: Persistable p = (Persistable) iter.next();
153:
154: page.load(new IntKey(p.getInt("page_id")));
155:
156: Link link = new Link();
157: link.setTitle(page.getString("title"));
158: link.setAlias(page.getString("aliasname"));
159: link.setHidden(page.getBoolean("hidden"));
160: link.setPageId(page.getInt("id"));
161:
162: linkMap.put(p.get("page_id"), link);
163: }
164:
165: //
166: // organize the links into the hierarchy
167: //
168: for (Iterator iter = l.iterator(); iter.hasNext();) {
169: Persistable p = (Persistable) iter.next();
170:
171: Link link = (Link) linkMap.get(p.get("page_id"));
172:
173: if (p.getInt("parent_id") == 0) {
174: root_ = link;
175: } else {
176: Link parent = (Link) linkMap.get(p.get("parent_id"));
177: parent.add(link);
178: }
179: }
180: }
181:
182: // properties ///////////////////////////////////////////////////////////////
183:
184: // attributes ///////////////////////////////////////////////////////////////
185:
186: protected SiteContext siteContext_ = null;
187: }
|