Skip to content

Commit

Permalink
Added ability to read files from dotCMS, not only from the harddisk.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasindilekci committed Sep 26, 2016
1 parent 325b19f commit 9cc90e2
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 42 deletions.
2 changes: 1 addition & 1 deletion conf/toolbox-ext.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<tool>
<key>excel</key>
<scope>application</scope>
<scope>request</scope>
<class>nl.isaac.dotcms.excelreader.viewtool.ExcelReaderTool</class>
</tool>

Binary file added lib/SuperCSV-with_src-1.52.jar
Binary file not shown.
28 changes: 14 additions & 14 deletions src/nl/isaac/dotcms/excelreader/shared/CacheGroupHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,58 @@
*
* @param <T>
*/
public class CacheGroupHandler<T> {
public class CacheGroupHandler<K, V> {
private String groupName;
protected ItemHandler<T> itemHandler;
protected ItemHandler<K, V> itemHandler;

public CacheGroupHandler(String groupName, ItemHandler<T> itemHandler) {
public CacheGroupHandler(String groupName, ItemHandler<K, V> itemHandler) {
this.groupName = groupName;
this.itemHandler = itemHandler;
}

public T get(String key) {
public V get(K key) {
DotCacheAdministrator cache = CacheLocator.getCacheAdministrator();
Object o = null;

if(!itemHandler.isChanged(key)) {
try {
o = cache.get(key, groupName);
o = cache.get(key.toString(), groupName);
} catch (DotCacheException e) {
Logger.info(this.getClass(), String.format("DotCacheException for Group '%s', key '%s', message: %s", groupName, key, e.getMessage()));
}
}

if(o == null) {
T t = itemHandler.get(key);
V t = itemHandler.get(key);
put(key, t);
return t;
} else {
return (T)o;
return (V)o;
}
}

public void put(String key, T t) {
public void put(K key, V t) {
DotCacheAdministrator cache = CacheLocator.getCacheAdministrator();
cache.put(key, t, groupName);
cache.put(key.toString(), t, groupName);
}

/**
* Updates the given key by calling the itemhandler's get method
*/
public void updateWithItemHandler(String key) {
public void updateWithItemHandler(K key) {
remove(key);
put(key, itemHandler.get(key));
}

public void remove(String key) {
public void remove(K key) {
DotCacheAdministrator cache = CacheLocator.getCacheAdministrator();
cache.remove(key, groupName);
cache.remove(key.toString(), groupName);
}

public void fillInitialCache() {
removeAll();
Map<String, T> initialCache = itemHandler.getInitialCache();
for(Entry<String, T> entry: initialCache.entrySet()) {
Map<K, V> initialCache = itemHandler.getInitialCache();
for(Entry<K, V> entry: initialCache.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/nl/isaac/dotcms/excelreader/shared/ItemHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*
* @param <T>
*/
public interface ItemHandler<T> {
public T get(String key);
public boolean isChanged(String key);
public Map<String,T> getInitialCache();
public interface ItemHandler<K, V> {
public V get(K key);
public boolean isChanged(K key);
public Map<K,V> getInitialCache();
}
36 changes: 36 additions & 0 deletions src/nl/isaac/dotcms/excelreader/shared/RequestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nl.isaac.dotcms.excelreader.shared;

import javax.servlet.http.HttpServletRequest;

import com.dotmarketing.util.WebKeys;

public class RequestUtil {
public static boolean isLiveMode(HttpServletRequest request) {
return !(isEditMode(request) || isPreviewMode(request));
}

public static boolean isEditMode(HttpServletRequest request) {
Object EDIT_MODE_SESSION = request.getSession().getAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION);
if(EDIT_MODE_SESSION != null) {
return Boolean.valueOf(EDIT_MODE_SESSION.toString());
}
return false;
}

public static boolean isPreviewMode(HttpServletRequest request) {
Object PREVIEW_MODE_SESSION = request.getSession().getAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION);
if(PREVIEW_MODE_SESSION != null) {
return Boolean.valueOf(PREVIEW_MODE_SESSION.toString());
}
return false;
}

public static Integer getLanguage(HttpServletRequest request) {
return (Integer)request.getSession().getAttribute(WebKeys.HTMLPAGE_LANGUAGE);
}

public static Integer getSelectedLanguage(HttpServletRequest request) {
return (Integer)request.getSession().getAttribute(WebKeys.LANGUAGE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author xander
*
*/
public class ExcelReaderCacheGroupHandler extends CacheGroupHandler<List<Map<String, Object>>> {
public class ExcelReaderCacheGroupHandler extends CacheGroupHandler<ExcelReaderFileKey, List<Map<String, Object>>> {
private static ExcelReaderCacheGroupHandler cache;

private ExcelReaderCacheGroupHandler() {
Expand Down
39 changes: 39 additions & 0 deletions src/nl/isaac/dotcms/excelreader/util/ExcelReaderDotCMSFileKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package nl.isaac.dotcms.excelreader.util;

import com.dotmarketing.beans.Host;

public class ExcelReaderDotCMSFileKey extends ExcelReaderFileKey {
private final Host host;
private final boolean live;

public ExcelReaderDotCMSFileKey(String path, Host host, boolean live) {
super(path);
this.host = host;
this.live = live;
}

public Host getHost() {
return host;
}

public boolean isLive() {
return live;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof ExcelReaderDotCMSFileKey) {
ExcelReaderDotCMSFileKey key = (ExcelReaderDotCMSFileKey)obj;
return super.equals(key) &&
key.getHost().getHostname().equals(getHost().getHostname()) &&
(key.isLive() == isLive());
}
return false;
}

@Override
public String toString() {
return super.toString() + "_" + getHost().getHostname() + "_" + isLive();
}

}
32 changes: 32 additions & 0 deletions src/nl/isaac/dotcms/excelreader/util/ExcelReaderFileKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nl.isaac.dotcms.excelreader.util;

public class ExcelReaderFileKey {
private final String path;

public ExcelReaderFileKey(String path) {
this.path = path;
}

public String getPath() {
return path;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof ExcelReaderFileKey) {
return ((ExcelReaderFileKey)obj).getPath().equals(getPath());
}
return false;
}

@Override
public int hashCode() {
return getPath().hashCode();
}

@Override
public String toString() {
return getPath();
}

}
69 changes: 56 additions & 13 deletions src/nl/isaac/dotcms/excelreader/util/ExcelReaderItemHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
* @copyright Copyright (c) 2011 ISAAC Software Solutions B.V. (http://www.isaac.nl)
*/

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.dotmarketing.business.APILocator;
import com.dotmarketing.portlets.files.factories.FileFactory;
import com.dotmarketing.util.Logger;

import nl.isaac.dotcms.excelreader.shared.ItemHandler;
Expand All @@ -25,17 +31,25 @@
* @author xander
*/

public class ExcelReaderItemHandler implements ItemHandler<List<Map<String, Object>>> {
private Map<String, Long> lastModDates = new HashMap<String, Long>();
public class ExcelReaderItemHandler implements ItemHandler<ExcelReaderFileKey, List<Map<String, Object>>> {
private Map<ExcelReaderFileKey, Long> lastModDates = new HashMap<ExcelReaderFileKey, Long>();

/**
* @return whether the key has changed since it was last requested
*/
public boolean isChanged(String key) {
public boolean isChanged(ExcelReaderFileKey key) {
try {
if(lastModDates.containsKey(key)) {
File file = new File(key);
return file == null || !lastModDates.get(key).equals(file.lastModified());
if(key instanceof ExcelReaderDotCMSFileKey) {
//get from dotCMS
ExcelReaderDotCMSFileKey dotcmsKey = (ExcelReaderDotCMSFileKey)key;
com.dotmarketing.portlets.files.model.File file = FileFactory.getFileByURI(dotcmsKey.getPath(), dotcmsKey.getHost(), dotcmsKey.isLive());
return file == null || !lastModDates.get(key).equals(file.getModDate().getTime());
} else {
//get from the file system
File file = new File(key.getPath());
return file == null || !lastModDates.get(key).equals(file.lastModified());
}
} else {
return true;
}
Expand All @@ -48,26 +62,55 @@ public boolean isChanged(String key) {
/**
* @return an excel sheet as a List of Maps. The key is the location of the file (on the harddisk)
*/
public List<Map<String, Object>> get(String key) {
public List<Map<String, Object>> get(ExcelReaderFileKey key) {
Calendar start = Calendar.getInstance();
InputStream is = null;
File file = null;
com.dotmarketing.portlets.files.model.File dotcmsFile = null;
try {
File file = new File(key);
FileInputStream fis = new FileInputStream(file);
if(key instanceof ExcelReaderDotCMSFileKey) {
//get from dotCMS
ExcelReaderDotCMSFileKey dotcmsKey = (ExcelReaderDotCMSFileKey)key;
dotcmsFile = FileFactory.getFileByURI(dotcmsKey.getPath(), dotcmsKey.getHost(), dotcmsKey.isLive());
is = new ByteArrayInputStream(FileFactory.getFileData(dotcmsFile));
} else {
//get from the file system
file = new File(key.getPath());
is = new FileInputStream(file);
}

DefaultRowStrategy strategy = new DefaultRowStrategy();
ExcelUtilStatus status = new ExcelUtilStatus();
ExcelUtil.executeStrategyOnExcelSheet(fis, strategy, status);
ExcelUtil.executeStrategyOnExcelSheet(is, strategy, status);
Logger.info(this.getClass(), "Reading of excel '" + key + "' took " + (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis()) + "ms");
lastModDates.put(key, file.lastModified());

if(key instanceof ExcelReaderDotCMSFileKey) {
lastModDates.put(key, dotcmsFile.getModDate().getTime());
} else {
lastModDates.put(key, file.lastModified());
}
for(Entry<Integer, Exception> entry: status.getMapWithRowNumbersAndExceptions().entrySet()) {
Logger.info(this.getClass(), "Row " + entry.getKey() + " has error: " + entry.getValue().getMessage());
}
Logger.info(this.getClass(), "Successfully read " + status.getNumberOfImportedRows() + " row and skipped " + status.getNumberOfFailedRows());
return strategy.getData();
} catch (Throwable t) {
Logger.error(this.getClass(), "Error while reading excel", t);
return null;
} finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
Logger.warn(this.getClass(), "Unable to close file connection to " + key.getPath());
}
}
}
}


public Map<String, List<Map<String, Object>>> getInitialCache() {
return new HashMap<String, List<Map<String, Object>>>();
@Override
public Map<ExcelReaderFileKey, List<Map<String, Object>>> getInitialCache() {
return new HashMap<ExcelReaderFileKey, List<Map<String, Object>>>();
}

}
Loading

0 comments on commit 9cc90e2

Please sign in to comment.