Skip to content

Commit

Permalink
Version 1.9.1 added
Browse files Browse the repository at this point in the history
  • Loading branch information
yasindilekci committed Sep 26, 2016
1 parent 542cb24 commit a59626a
Show file tree
Hide file tree
Showing 12 changed files with 565 additions and 0 deletions.
5 changes: 5 additions & 0 deletions MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Deploy-Class:
Plugin-Name: Excel Reader
Plugin-Version: 1.0
Author: Xander
3 changes: 3 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<project name="Excel Reader" default="build">
<import file="../common.xml"/>
</project>
6 changes: 6 additions & 0 deletions conf/toolbox-ext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<tool>
<key>excel</key>
<scope>application</scope>
<class>nl.isaac.dotcms.excelreader.viewtool.ExcelReaderTool</class>
</tool>

Binary file added lib/poi-ooxml-3.6-20091214.jar
Binary file not shown.
86 changes: 86 additions & 0 deletions src/nl/isaac/dotcms/excelreader/shared/CacheGroupHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package nl.isaac.dotcms.excelreader.shared;
/**
* ExcelReader by ISAAC - The Full Service Internet Agency is licensed
* under a Creative Commons Attribution 3.0 Unported License
* - http://creativecommons.org/licenses/by/3.0/
* - http://www.geekyplugins.com/
*
* @copyright Copyright (c) 2011 ISAAC Software Solutions B.V. (http://www.isaac.nl)
*/

import java.util.Map;
import java.util.Map.Entry;

import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.business.DotCacheAdministrator;
import com.dotmarketing.business.DotCacheException;
import com.dotmarketing.util.Logger;
/**
* Class that handles the dotCMS cache. It uses an ItemHandler<T> to retrieve items that aren't stored in the cache yet
*
* @author xander
*
* @param <T>
*/
public class CacheGroupHandler<T> {
private String groupName;
protected ItemHandler<T> itemHandler;

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

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

if(!itemHandler.isChanged(key)) {
try {
o = cache.get(key, 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);
put(key, t);
return t;
} else {
return (T)o;
}
}

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

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

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

public void fillInitialCache() {
removeAll();
Map<String, T> initialCache = itemHandler.getInitialCache();
for(Entry<String, T> entry: initialCache.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}

public void removeAll() {
DotCacheAdministrator cache = CacheLocator.getCacheAdministrator();
cache.flushGroup(groupName);
}

}
24 changes: 24 additions & 0 deletions src/nl/isaac/dotcms/excelreader/shared/ItemHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nl.isaac.dotcms.excelreader.shared;
/**
* ExcelReader by ISAAC - The Full Service Internet Agency is licensed
* under a Creative Commons Attribution 3.0 Unported License
* - http://creativecommons.org/licenses/by/3.0/
* - http://www.geekyplugins.com/
*
* @copyright Copyright (c) 2011 ISAAC Software Solutions B.V. (http://www.isaac.nl)
*/

import java.util.Map;

/**
* Interface for handling an item that is not available in the cache
*
* @author xander
*
* @param <T>
*/
public interface ItemHandler<T> {
public T get(String key);
public boolean isChanged(String key);
public Map<String,T> getInitialCache();
}
33 changes: 33 additions & 0 deletions src/nl/isaac/dotcms/excelreader/util/DefaultRowStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package nl.isaac.dotcms.excelreader.util;
/**
* ExcelReader by ISAAC - The Full Service Internet Agency is licensed
* under a Creative Commons Attribution 3.0 Unported License
* - http://creativecommons.org/licenses/by/3.0/
* - http://www.geekyplugins.com/
*
* @copyright Copyright (c) 2011 ISAAC Software Solutions B.V. (http://www.isaac.nl)
*/

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import nl.isaac.dotcms.excelreader.util.ExcelUtil.RowStrategy;
/**
* This row strategy just returns given row
*
* @author xander
*
*/
public class DefaultRowStrategy implements RowStrategy {
private List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

public void executeRow(Map<String, Object> row) throws Exception {
data.add(row);
}

public List<Map<String, Object>> getData() {
return data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nl.isaac.dotcms.excelreader.util;
/**
* ExcelReader by ISAAC - The Full Service Internet Agency is licensed
* under a Creative Commons Attribution 3.0 Unported License
* - http://creativecommons.org/licenses/by/3.0/
* - http://www.geekyplugins.com/
*
* @copyright Copyright (c) 2011 ISAAC Software Solutions B.V. (http://www.isaac.nl)
*/

import java.util.List;
import java.util.Map;

import nl.isaac.dotcms.excelreader.shared.CacheGroupHandler;
/**
* This class handles the cache for the Excelreader plugin
*
* @author xander
*
*/
public class ExcelReaderCacheGroupHandler extends CacheGroupHandler<List<Map<String, Object>>> {
private static ExcelReaderCacheGroupHandler cache;

private ExcelReaderCacheGroupHandler() {
super("ExcelReader_plugin", new ExcelReaderItemHandler());
}

public static ExcelReaderCacheGroupHandler getInstance() {
if(cache == null) {
cache = new ExcelReaderCacheGroupHandler();
}
return cache;
}


}
73 changes: 73 additions & 0 deletions src/nl/isaac/dotcms/excelreader/util/ExcelReaderItemHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package nl.isaac.dotcms.excelreader.util;
/**
* ExcelReader by ISAAC - The Full Service Internet Agency is licensed
* under a Creative Commons Attribution 3.0 Unported License
* - http://creativecommons.org/licenses/by/3.0/
* - http://www.geekyplugins.com/
*
* @copyright Copyright (c) 2011 ISAAC Software Solutions B.V. (http://www.isaac.nl)
*/

import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dotmarketing.util.Logger;

import nl.isaac.dotcms.excelreader.shared.ItemHandler;

/**
* A class that handles the importing of a single Excel file. Using the CacheGroupHandler/ItemHandler classes, the result is stored in the dotCMS cache.
*
* @author xander
*/

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

/**
* @return whether the key has changed since it was last requested
*/
public boolean isChanged(String key) {
try {
if(lastModDates.containsKey(key)) {
File file = new File(key);
return file == null || !lastModDates.get(key).equals(file.lastModified());
} else {
return true;
}
} catch (Throwable t) {
Logger.warn(this.getClass(), "Exception while checking date in file '" + key + "'", t);
return false;
}
}

/**
* @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) {
Calendar start = Calendar.getInstance();
try {
File file = new File(key);
FileInputStream fis = new FileInputStream(file);
DefaultRowStrategy strategy = new DefaultRowStrategy();
ExcelUtilStatus status = new ExcelUtilStatus();
ExcelUtil.executeStrategyOnExcelSheet(fis, strategy, status);
Logger.info(this.getClass(), "Reading of excel '" + key + "' took " + (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis()) + "ms");
lastModDates.put(key, file.lastModified());
return strategy.getData();
} catch (Throwable t) {
Logger.error(this.getClass(), "Error while reading excel", t);
return null;
}
}


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

}
Loading

0 comments on commit a59626a

Please sign in to comment.