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.io.Serializable;
024: import javax.servlet.http.HttpServletRequest;
025:
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.struts.action.ActionErrors;
028: import org.apache.struts.action.ActionError;
029: import org.apache.struts.validator.DynaValidatorForm;
030: import java.util.List;
031: import java.util.ArrayList;
032: import java.util.Arrays;
033: import java.io.File;
034: import org.apache.struts.util.LabelValueBean;
035: import com.methodhead.util.Comparators;
036: import com.methodhead.sitecontext.SiteContext;
037: import com.methodhead.auth.AuthUtil;
038:
039: public final class EditorForm extends DynaValidatorForm implements
040: Serializable {
041:
042: /**
043: * Recursively adds path options for <tt>dir</tt> to <tt>options</tt>.
044: */
045: protected void getPathOptions(List options, String path, File dir) {
046:
047: //
048: // handle the root directory specially
049: //
050: if (path == null)
051: options.add(new LabelValueBean("/", ""));
052: else if ("".equals(path))
053: options.add(new LabelValueBean("/" + dir.getName(), dir
054: .getName()));
055: else
056: options.add(new LabelValueBean("/" + path + "/"
057: + dir.getName(), path + "/" + dir.getName()));
058:
059: File[] files = dir.listFiles();
060: if (files == null)
061: throw new ShimException("Couldn't list files for \"" + dir
062: + "\"");
063:
064: Arrays.sort(files, Comparators.fileComparator());
065:
066: for (int i = 0; i < files.length; i++) {
067: if (files[i].isDirectory()) {
068: if ("thumbs".equals(files[i].getName()))
069: continue;
070:
071: if (path == null)
072: getPathOptions(options, "", files[i]);
073: else if ("".equals(path))
074: getPathOptions(options, dir.getName(), files[i]);
075: else
076: getPathOptions(options, path + "/" + dir.getName(),
077: files[i]);
078: }
079: }
080: }
081:
082: public void reset(ActionMapping mapping, HttpServletRequest request) {
083:
084: //
085: // don't do anything unless someone is logged in
086: //
087: if (AuthUtil.getUser(request) == null)
088: return;
089:
090: //
091: // get the site context root directory
092: //
093: SiteContext siteContext = SiteContext.getContext(request);
094:
095: File dir = new File(request.getSession().getServletContext()
096: .getRealPath("/" + siteContext.getInt("id")));
097:
098: //
099: // add the options
100: //
101: List options = new ArrayList();
102: getPathOptions(options, null, dir);
103: set("paths", options);
104: }
105:
106: public ActionErrors validate(ActionMapping mapping,
107: HttpServletRequest request) {
108:
109: return super.validate(mapping, request);
110: }
111: }
|