Skip to content

Commit

Permalink
Updated the code for dotCMS 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
yasindilekci committed Sep 26, 2016
1 parent 9cc90e2 commit 4305836
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 21 deletions.
29 changes: 29 additions & 0 deletions src/nl/isaac/dotcms/excelreader/shared/FileTools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nl.isaac.dotcms.excelreader.shared;

import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.fileassets.business.FileAsset;
import com.liferay.portal.model.User;

public class FileTools {

public static FileAsset getFileAssetByURI(String URI, Host host, boolean isLive) {
try {
User systemUser = APILocator.getUserAPI().getSystemUser();
long defaultLanguage = APILocator.getLanguageAPI().getDefaultLanguage().getId();

Identifier identifier = APILocator.getIdentifierAPI().find(host, URI);
Contentlet fileContentlet = APILocator.getContentletAPI().findContentletByIdentifier(identifier.getId(), isLive, defaultLanguage, systemUser, false );

return APILocator.getFileAssetAPI().fromContentlet(fileContentlet);
} catch (DotSecurityException e) {
throw new RuntimeException(e);
} catch (DotDataException e) {
throw new RuntimeException(e);
}
}
}
16 changes: 15 additions & 1 deletion src/nl/isaac/dotcms/excelreader/util/DefaultRowStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@
*/
public class DefaultRowStrategy implements RowStrategy {
private List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
private boolean skipEmptyLines = false;
public DefaultRowStrategy() {}
public DefaultRowStrategy(boolean skipEmptyLines) {
this.skipEmptyLines = skipEmptyLines;
}

public void executeRow(Map<String, Object> row) throws Exception {
data.add(row);
if (skipEmptyLines) {
for (Map.Entry<String, Object> value : row.entrySet()) {
if (value.getValue() != null && (!(value.getValue() instanceof String) || ((String) value.getValue()).length() > 0)) {
data.add(row);
return;
}
}
} else {
data.add(row);
}
}

public List<Map<String, Object>> getData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
public class ExcelReaderDotCMSFileKey extends ExcelReaderFileKey {
private final Host host;
private final boolean live;
public ExcelReaderDotCMSFileKey(String path, Host host, boolean live) {
super(path);

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

public Host getHost() {
return host;
}

public boolean isLive() {
return live;
}
Expand Down
9 changes: 7 additions & 2 deletions src/nl/isaac/dotcms/excelreader/util/ExcelReaderFileKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

public class ExcelReaderFileKey {
private final String path;
private final boolean skipEmptyLines;

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

public String getPath() {
return path;
}
public boolean isSkipEmptyLines() {
return skipEmptyLines;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof ExcelReaderFileKey) {
return ((ExcelReaderFileKey)obj).getPath().equals(getPath());
return ((ExcelReaderFileKey)obj).getPath().equals(getPath()) && skipEmptyLines == ((ExcelReaderFileKey) obj).isSkipEmptyLines();
}
return false;
}
Expand Down
19 changes: 9 additions & 10 deletions src/nl/isaac/dotcms/excelreader/util/ExcelReaderItemHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* @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;
Expand All @@ -19,12 +18,12 @@
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.FileTools;
import nl.isaac.dotcms.excelreader.shared.ItemHandler;

import com.dotmarketing.portlets.fileassets.business.FileAsset;
import com.dotmarketing.util.Logger;

/**
* A class that handles the importing of a single Excel file. Using the CacheGroupHandler/ItemHandler classes, the result is stored in the dotCMS cache.
*
Expand All @@ -43,7 +42,7 @@ public boolean isChanged(ExcelReaderFileKey key) {
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());
FileAsset file = FileTools.getFileAssetByURI(dotcmsKey.getPath(), dotcmsKey.getHost(), dotcmsKey.isLive());
return file == null || !lastModDates.get(key).equals(file.getModDate().getTime());
} else {
//get from the file system
Expand All @@ -66,20 +65,20 @@ 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;
FileAsset dotcmsFile = null;
try {
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));
dotcmsFile = FileTools.getFileAssetByURI(dotcmsKey.getPath(), dotcmsKey.getHost(), dotcmsKey.isLive());
is = dotcmsFile.getFileInputStream();
} else {
//get from the file system
file = new File(key.getPath());
is = new FileInputStream(file);
}

DefaultRowStrategy strategy = new DefaultRowStrategy();
DefaultRowStrategy strategy = new DefaultRowStrategy(key.isSkipEmptyLines());
ExcelUtilStatus status = new ExcelUtilStatus();
ExcelUtil.executeStrategyOnExcelSheet(is, strategy, status);
Logger.info(this.getClass(), "Reading of excel '" + key + "' took " + (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis()) + "ms");
Expand Down
1 change: 0 additions & 1 deletion src/nl/isaac/dotcms/excelreader/util/ExcelUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down
11 changes: 8 additions & 3 deletions src/nl/isaac/dotcms/excelreader/viewtool/ExcelReaderTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.velocity.tools.view.tools.ViewTool;

import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
Expand All @@ -49,12 +48,18 @@ public void init(Object arg0) {
/**
* Get the information of an Excel file as a list of maps.
*/
public List<Map<String, Object>> readExcel(String file, boolean skipEmptyLines) {
return ExcelReaderCacheGroupHandler.getInstance().get(new ExcelReaderFileKey(file, skipEmptyLines));
}
public List<Map<String, Object>> readExcel(String file) {
return ExcelReaderCacheGroupHandler.getInstance().get(new ExcelReaderFileKey(file));
return readExcel(file, false);
}

public List<Map<String, Object>> readExcelFromDotCMS(String url) {
return ExcelReaderCacheGroupHandler.getInstance().get(new ExcelReaderDotCMSFileKey(url, getCurrentHost(), isLive()));
return readExcelFromDotCMS(url, false);
}
public List<Map<String, Object>> readExcelFromDotCMS(String url, boolean skipEmptyLines) {
return ExcelReaderCacheGroupHandler.getInstance().get(new ExcelReaderDotCMSFileKey(url, getCurrentHost(), isLive(), skipEmptyLines));
}

/**
Expand Down

0 comments on commit 4305836

Please sign in to comment.