001: package org.contineo.core.transfer;
002:
003: import java.io.BufferedInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.util.Collection;
010: import java.util.Iterator;
011: import java.util.StringTokenizer;
012: import java.util.zip.ZipEntry;
013: import java.util.zip.ZipOutputStream;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.contineo.core.security.Menu;
018: import org.contineo.core.security.dao.MenuDAO;
019: import org.contineo.util.Context;
020: import org.contineo.util.config.SettingsConfig;
021:
022: /**
023: * Exports a folder hierarchie and all documents in it as a zip file.
024: *
025: * @author Alessandro Gasparini
026: */
027: public class ZipExport implements Export {
028:
029: protected static Log log = LogFactory.getLog(ZipExport.class);
030:
031: private ZipOutputStream zos;
032:
033: private String username;
034:
035: private boolean allLevel;
036:
037: private int startMenuId;
038:
039: public ZipExport() {
040: zos = null;
041: username = "";
042: allLevel = false;
043: startMenuId = 0;
044: }
045:
046: /**
047: * @see org.contineo.core.export.Export#process(org.contineo.core.security.Menu,
048: * java.lang.String)
049: */
050: public ByteArrayOutputStream process(Menu menu, String user)
051: throws IOException {
052: this .username = user;
053: this .startMenuId = menu.getMenuId();
054:
055: ByteArrayOutputStream bos = new ByteArrayOutputStream();
056: zos = new ZipOutputStream(bos);
057: appendChildren(menu, 0);
058: zos.flush();
059: zos.close();
060: return bos;
061: }
062:
063: /**
064: * @see org.contineo.core.export.Export#process(int, java.lang.String)
065: */
066: public ByteArrayOutputStream process(int menuId, String user)
067: throws IOException {
068: MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
069: MenuDAO.class);
070: Menu menu = menuDao.findByPrimaryKey(menuId);
071: return process(menu, user);
072: }
073:
074: /**
075: * If allLevel set true all children of a specified menu will be export.
076: * Otherwise only the first level will be export.
077: *
078: * @param b
079: */
080: public void setAllLevel(boolean b) {
081: allLevel = b;
082: }
083:
084: protected void appendChildren(Menu menu, int level) {
085:
086: if (!allLevel && (level > 1)) {
087: return;
088: } else {
089: if (menu.getMenuType() == Menu.MENUTYPE_FILE) {
090: String menupath = menu.getMenuPath();
091: SettingsConfig settings = (SettingsConfig) Context
092: .getInstance().getBean(SettingsConfig.class);
093: String filepath = settings.getValue("docdir");
094: filepath += menupath + "/"
095: + String.valueOf(menu.getMenuId()) + "/";
096: filepath += menu.getMenuRef();
097: addFile(menu, filepath);
098: }
099:
100: MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
101: MenuDAO.class);
102: Collection children = menuDao.findByUserName(username, menu
103: .getMenuId());
104: Iterator iter = children.iterator();
105:
106: while (iter.hasNext()) {
107: appendChildren((Menu) iter.next(), level + 1);
108: }
109: }
110: }
111:
112: protected void addFile(Menu menu, String filepath) {
113: try {
114: File currentFile = new File(filepath);
115: InputStream is = new FileInputStream(currentFile);
116: BufferedInputStream bis = new BufferedInputStream(is);
117:
118: // Create the path as the user see in contineo
119: String decodedMenuPath = decodeMenuPath(menu.getMenuPath());
120:
121: ZipEntry entry = new ZipEntry(decodedMenuPath + "/"
122: + currentFile.getName());
123: zos.putNextEntry(entry);
124:
125: // Transfer bytes from the file to the ZIP file
126: int len;
127: while ((len = bis.read()) != -1) {
128: zos.write(len);
129: }
130:
131: bis.close();
132: } catch (Exception e) {
133: log.error(e.getMessage(), e);
134: }
135: }
136:
137: private String decodeMenuPath(String menuPath) {
138:
139: String decodedPath = "";
140:
141: MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
142: MenuDAO.class);
143:
144: // substring the menuPath with the startMenuId
145: int startIndx = menuPath.indexOf(String.valueOf(startMenuId));
146: menuPath = menuPath.substring(startIndx);
147:
148: StringTokenizer st = new StringTokenizer(menuPath, "/");
149:
150: while (st.hasMoreTokens()) {
151: String menuId = st.nextToken();
152:
153: Menu menu = menuDao.findByPrimaryKey(Integer
154: .parseInt(menuId));
155: if (menu != null)
156: decodedPath += "/" + menu.getMenuText();
157: }
158:
159: return decodedPath;
160: }
161:
162: }
|