}
//GetPrevious version if exists
IFileAsset currentFile = null;
Identifier currentId = APILocator.getIdentifierAPI().find(host, pluginFolderPath+"/"+filePathAndName);
if(currentId!=null && InodeUtils.isSet(currentId.getId()) && currentId.getAssetType().equals("contentlet")){
Contentlet cont = APILocator.getContentletAPI().findContentletByIdentifier(currentId.getId(), true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), APILocator.getUserAPI().getSystemUser(),false);
if(cont!=null && InodeUtils.isSet(cont.getInode())){
currentFile = APILocator.getFileAssetAPI().fromContentlet(cont);
cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(fileName));
cont.setFolder(folder.getInode());
cont.setHost(host.getIdentifier());
cont.setBinary(FileAssetAPI.BINARY_FIELD, temporaryFile);
APILocator.getContentletAPI().checkin(cont, APILocator.getUserAPI().getSystemUser(),false);
APILocator.getVersionableAPI().setWorking(cont);
APILocator.getVersionableAPI().setLive(cont);
if (cont.isLive()){
LiveCache.removeAssetFromCache(cont);
LiveCache.addToLiveAssetToCache(cont);
}else{
LiveCache.removeAssetFromCache(cont);
LiveCache.addToLiveAssetToCache(cont);
}
WorkingCache.removeAssetFromCache(cont);
WorkingCache.addToWorkingAssetToCache(cont);
}
}else if(currentId!=null && InodeUtils.isSet(currentId.getId())){
currentFile = APILocator.getFileAPI().getFileByURI(pluginFolderPath+"/"+filePathAndName, host, true, APILocator.getUserAPI().getSystemUser(),false);
com.dotmarketing.portlets.files.model.File file = new com.dotmarketing.portlets.files.model.File();
file.setFileName(fileName);
file.setFriendlyName(UtilMethods.getFileName(fileName));
file.setTitle(UtilMethods.getFileName(fileName));
file.setMimeType(APILocator.getFileAPI().getMimeType(fileName));
file.setOwner(systemUser.getUserId());
file.setModUser(systemUser.getUserId());
file.setModDate(new Date());
file.setParent(folder.getIdentifier());
file.setSize((int)temporaryFile.length());
HibernateUtil.saveOrUpdate(file);
APILocator.getFileAPI().invalidateCache(file);
// get the file Identifier
Identifier ident = null;
if (InodeUtils.isSet(currentFile.getInode())){
ident = APILocator.getIdentifierAPI().find((com.dotmarketing.portlets.files.model.File)currentFile);
APILocator.getFileAPI().invalidateCache((com.dotmarketing.portlets.files.model.File)currentFile);
}else{
ident = new Identifier();
}
//Saving the file, this creates the new version and save the new data
com.dotmarketing.portlets.files.model.File workingFile = null;
workingFile = APILocator.getFileAPI().saveFile(file, temporaryFile, folder, systemUser, false);
APILocator.getVersionableAPI().setWorking(workingFile);
APILocator.getVersionableAPI().setLive(workingFile);
APILocator.getFileAPI().invalidateCache(workingFile);
ident = APILocator.getIdentifierAPI().find(workingFile);
//updating caches
if (workingFile.isLive()){
LiveCache.removeAssetFromCache(workingFile);
LiveCache.addToLiveAssetToCache(workingFile);
}else{
LiveCache.removeAssetFromCache(file);
LiveCache.addToLiveAssetToCache(file);
}
WorkingCache.removeAssetFromCache(workingFile);
WorkingCache.addToWorkingAssetToCache(workingFile);
//Publish the File
PublishFactory.publishAsset(workingFile, systemUser, false);
}else{
Contentlet cont = new Contentlet();
cont.setStructureInode(folder.getDefaultFileType());
cont.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(fileName));
cont.setFolder(folder.getInode());
cont.setHost(host.getIdentifier());
cont.setBinary(FileAssetAPI.BINARY_FIELD, temporaryFile);
APILocator.getContentletAPI().checkin(cont, APILocator.getUserAPI().getSystemUser(),false);
APILocator.getVersionableAPI().setWorking(cont);
APILocator.getVersionableAPI().setLive(cont);
if (cont.isLive()){
LiveCache.removeAssetFromCache(cont);
LiveCache.addToLiveAssetToCache(cont);
}else{
LiveCache.removeAssetFromCache(cont);
LiveCache.addToLiveAssetToCache(cont);