@Override
protected void processEvent(Event<UIGoogleDocsCheckinAction> event) throws Exception {
googleDriveService = (GoogleDriveService) PortalContainer.getInstance().getComponentInstanceOfType(GoogleDriveService.class);
UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
Node currentNode = uiExplorer.getCurrentNode();
try {
if (currentNode.isNodeType(GoogleDocsConstants.GOOGLE_DRIVE_NODE_TYPE)
&& currentNode.getProperty(GoogleDocsConstants.GOOGLE_DRIVE_NODE_PROPERTY) != null
&& !currentNode.getProperty(GoogleDocsConstants.GOOGLE_DRIVE_NODE_PROPERTY).getString().isEmpty()) {
// get Google Drive file id
String fileId = currentNode.getProperty(GoogleDocsConstants.GOOGLE_DRIVE_NODE_PROPERTY).getString();
Node contentNode = currentNode.getNode("jcr:content");
String mimeType = contentNode.getProperty("jcr:mimeType").getString();
// store Google Drive document content in the node's data
InputStream fileInputStream = googleDriveService.getFileContent(fileId, mimeType);
if (fileInputStream != null) {
contentNode.getProperty("jcr:data").setValue(fileInputStream);
currentNode.save();
}
// remove the document from Google Drive
googleDriveService.removeFileInGoogleDrive(fileId);
// remove mixin
currentNode.removeMixin(GoogleDocsConstants.GOOGLE_DRIVE_NODE_TYPE);
currentNode.save();
// clear corresponding cache entry
CacheService cacheService = (CacheService) PortalContainer.getInstance().getComponentInstanceOfType(CacheService.class);
ExoCache<Serializable, Object> pdfCache = cacheService.getCacheInstance(PDFViewerRESTService.class.getName());
StringBuilder sbFileCacheKey = new StringBuilder();
sbFileCacheKey.append(((RepositoryImpl) currentNode.getSession().getRepository()).getName()) //
.append("/") //
.append(currentNode.getSession().getWorkspace().getName()) //
.append("/") //
.append(currentNode.getUUID());
final String fileCacheKey = sbFileCacheKey.toString();
// use selector to clear only the entries related to this document
CachedObjectSelector<Serializable, Object> selector = new CachedObjectSelector<Serializable, Object>() {
public boolean select(Serializable key, ObjectCacheInfo<? extends Object> ocinfo) {
String skey = key.toString();
if (skey.toString().startsWith(fileCacheKey)) {
return true;
}
return false;
}
public void onSelect(ExoCache<? extends Serializable, ? extends Object> cache, Serializable key,
ObjectCacheInfo<? extends Object> ocinfo) throws Exception {
cache.remove(key);
}
};
pdfCache.select(selector);
event.getRequestContext().addUIComponentToUpdateByAjax(uiExplorer.getChild(UIWorkingArea.class));
log.info("Document " + currentNode.getPath() + " has been succesfully checked in from Google Drive (Google Drive ID : " + fileId + ")");
} else {
log.warn("No Google Drive ID available on the document " + currentNode.getPath());
org.exoplatform.wcm.webui.Utils.createPopupMessage(uiExplorer, "UIActionBar.msg.googledocs.not-in-google-docs", null,