001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/archive/tags/sakai_2-4-1/archive-tool/tool/src/java/org/sakaiproject/archive/tool/ArchiveAction.java $
003: * $Id: ArchiveAction.java 9913 2006-05-25 03:06:15Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 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.sakaiproject.archive.tool;
021:
022: import java.util.Enumeration;
023: import java.util.Hashtable;
024:
025: import org.sakaiproject.archive.cover.ArchiveService;
026: import org.sakaiproject.authz.cover.SecurityService;
027: import org.sakaiproject.cheftool.Context;
028: import org.sakaiproject.cheftool.JetspeedRunData;
029: import org.sakaiproject.cheftool.RunData;
030: import org.sakaiproject.cheftool.VelocityPortlet;
031: import org.sakaiproject.cheftool.VelocityPortletPaneledAction;
032: import org.sakaiproject.cheftool.api.Menu;
033: import org.sakaiproject.cheftool.menu.MenuEntry;
034: import org.sakaiproject.cheftool.menu.MenuImpl;
035: import org.sakaiproject.event.api.SessionState;
036: import org.sakaiproject.id.cover.IdManager;
037: import org.sakaiproject.util.FileItem;
038: import org.sakaiproject.util.ResourceLoader;
039: import org.sakaiproject.util.StringUtil;
040:
041: /**
042: * <p>ArchiveAction is the Sakai archive tool.</p>
043: */
044: public class ArchiveAction extends VelocityPortletPaneledAction {
045: private static final String STATE_MODE = "mode";
046: private static final String BATCH_MODE = "batch";
047:
048: /** Resource bundle using current language locale */
049: private static ResourceLoader rb = new ResourceLoader("admin");
050:
051: /**
052: * build the context
053: */
054: public String buildMainPanelContext(VelocityPortlet portlet,
055: Context context, RunData rundata, SessionState state) {
056: String template = null;
057:
058: // if not logged in as the super user, we won't do anything
059: if (!SecurityService.isSuperUser()) {
060: context.put("tlang", rb);
061: return (String) getContext(rundata).get("template")
062: + "_noaccess";
063: }
064:
065: // check mode and dispatch
066: String mode = (String) state.getAttribute(STATE_MODE);
067: if (mode == null) {
068: template = buildListPanelContext(portlet, context, rundata,
069: state);
070: } else if (mode.equals(BATCH_MODE)) {
071: template = buildBatchPanelContext(portlet, context,
072: rundata, state);
073: }
074:
075: return (String) getContext(rundata).get("template") + template;
076:
077: } // buildMainPanelContext
078:
079: /**
080: * build the context for non-batch import/export
081: */
082: public String buildListPanelContext(VelocityPortlet portlet,
083: Context context, RunData rundata, SessionState state) {
084: context.put("tlang", rb);
085:
086: // build the menu
087: Menu bar = new MenuImpl();
088: bar.add(new MenuEntry(rb.getString("archive.button.batch"),
089: "doToggle_State"));
090: context.put(Menu.CONTEXT_MENU, bar);
091: context.put(Menu.CONTEXT_ACTION, "ArchiveAction");
092:
093: return "";
094:
095: } // buildListPanelContext
096:
097: /**
098: * build the context for batch import/export
099: */
100: public String buildBatchPanelContext(VelocityPortlet portlet,
101: Context context, RunData rundata, SessionState state) {
102: context.put("tlang", rb);
103:
104: //build the menu
105: Menu bar = new MenuImpl();
106: bar.add(new MenuEntry(rb.getString("archive.button.nonbatch"),
107: "doToggle_State"));
108: context.put(Menu.CONTEXT_MENU, bar);
109: context.put(Menu.CONTEXT_ACTION, "ArchiveAction");
110:
111: return "-batch";
112:
113: } // buildListPanelContext
114:
115: /**
116: *
117: */
118: public void doToggle_State(RunData data, Context context) {
119: String peid = ((JetspeedRunData) data).getJs_peid();
120: SessionState state = ((JetspeedRunData) data)
121: .getPortletSessionState(peid);
122:
123: if (state.getAttribute(STATE_MODE) == null) {
124: state.setAttribute(STATE_MODE, BATCH_MODE);
125: } else {
126: state.removeAttribute(STATE_MODE);
127: }
128:
129: } // doToggle_State
130:
131: /**
132: * doArchive called when "eventSubmit_doArchive" is in the request parameters
133: * to run the archive.
134: */
135: public void doArchive(RunData data, Context context) {
136: SessionState state = ((JetspeedRunData) data)
137: .getPortletSessionState(((JetspeedRunData) data)
138: .getJs_peid());
139:
140: if (!SecurityService.isSuperUser()) {
141: addAlert(state, rb.getString("archive.limited"));
142: return;
143: }
144:
145: String id = data.getParameters().getString("archive-id");
146: if ((id != null) && (id.trim().length() > 0)) {
147: String msg = ArchiveService.archive(id.trim());
148: addAlert(state, rb.getString("archive.site") + " " + id
149: + " " + rb.getString("archive.complete") + " "
150: + msg);
151: } else {
152: addAlert(state, rb.getString("archive.please"));
153: }
154:
155: } // doArchive
156:
157: /**
158: * doImport called when "eventSubmit_doImport" is in the request parameters
159: * to run an import.
160: */
161: public void doImport(RunData data, Context context) {
162: SessionState state = ((JetspeedRunData) data)
163: .getPortletSessionState(((JetspeedRunData) data)
164: .getJs_peid());
165:
166: if (!SecurityService.isSuperUser()) {
167: addAlert(state, rb.getString("archive.import"));
168: return;
169: }
170:
171: String id = data.getParameters().getString("import-id");
172: String file = data.getParameters().getString("import-file");
173: if ((id != null) && (id.trim().length() > 0) && (file != null)
174: && (file.trim().length() > 0)) {
175: String msg = ArchiveService.merge(file.trim(), id.trim(),
176: null);
177: addAlert(state, rb.getString("archive.import1") + " "
178: + file + " " + rb.getString("archive.site") + " "
179: + id + " " + rb.getString("archive.complete") + " "
180: + msg);
181: } else {
182: addAlert(state, rb.getString("archive.file"));
183: }
184:
185: } // doImport
186:
187: /**
188: * doImport called when "eventSubmit_doImport" is in the request parameters
189: * to run an import.
190: */
191: public void doBatch_Import(RunData data, Context context) {
192: SessionState state = ((JetspeedRunData) data)
193: .getPortletSessionState(((JetspeedRunData) data)
194: .getJs_peid());
195:
196: Hashtable fTable = new Hashtable();
197:
198: if (!SecurityService.isSuperUser()) {
199: addAlert(state, rb.getString("archive.batch.auth"));
200: return;
201: }
202:
203: //String fileName = data.getParameters().getString("import-file");
204: FileItem fi = data.getParameters().getFileItem("importFile");
205: if (fi == null) {
206: addAlert(state, rb.getString("archive.batch.missingname"));
207: } else {
208: // get content
209: String content = fi.getString();
210:
211: String[] lines = content.split("\n");
212: for (int i = 0; i < lines.length; i++) {
213: String lineContent = (String) lines[i];
214: String[] lineContents = lineContent.split("\t");
215: if (lineContents.length == 2) {
216: fTable.put(lineContents[0], lineContents[1]);
217: } else {
218: addAlert(state, rb
219: .getString("archive.batch.wrongformat"));
220: }
221: }
222: }
223:
224: if (!fTable.isEmpty()) {
225: Enumeration importFileName = fTable.keys();
226: int count = 1;
227: while (importFileName.hasMoreElements()) {
228: String path = StringUtil
229: .trimToNull((String) importFileName
230: .nextElement());
231: String siteCreatorName = StringUtil
232: .trimToNull((String) fTable.get(path));
233: if (path != null && siteCreatorName != null) {
234: String nSiteId = IdManager.createUuid();
235:
236: try {
237: // merge
238: addAlert(
239: state,
240: "\n"
241: + rb
242: .getString("archive.import1")
243: + " "
244: + count
245: + ": "
246: + rb
247: .getString("archive.import2")
248: + " "
249: + path
250: + " "
251: + rb.getString("archive.site")
252: + " "
253: + nSiteId
254: + " "
255: + rb
256: .getString("archive.importcreatorid")
257: + " "
258: + siteCreatorName
259: + " "
260: + rb
261: .getString("archive.complete")
262: + "\n");
263: addAlert(state, ArchiveService.merge(path,
264: nSiteId, siteCreatorName));
265:
266: } catch (Exception ignore) {
267: }
268: }
269:
270: count++;
271: }
272: }
273: } // doBatchImport
274:
275: } // ArchiveAction
|