String newFolderInode = (String) result.get("remote_inode");
String oldFolderIdentifier = (String) result.get("local_identifier");
String newFolderIdentifier = (String) result.get("remote_identifier");
try {
Folder folder = APILocator.getFolderAPI().find(oldFolderInode, APILocator.getUserAPI().getSystemUser(), false);
List<Contentlet> contents = APILocator.getContentletAPI().findContentletsByFolder(folder, APILocator.getUserAPI().getSystemUser(), false);
for (Contentlet contentlet : contents) {
APILocator.getContentletIndexAPI().removeContentFromIndex(contentlet);
CacheLocator.getContentletCache().remove(contentlet.getInode());
}
Identifier folderIdentifier = APILocator.getIdentifierAPI().find(folder.getIdentifier());
CacheLocator.getFolderCache().removeFolder(folder, folderIdentifier);
CacheLocator.getIdentifierCache().removeFromCacheByIdentifier(folderIdentifier.getId());
// THIS IS THE NEW CODE
// 1.1) Insert dummy temp row on INODE table
if ( DbConnectionFactory.isOracle() ) {
dc.executeStatement( "insert into inode values ('TEMP_INODE', 'DUMMY_OWNER', to_date('1900-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), 'DUMMY_TYPE') " );
} else {
dc.executeStatement( "insert into inode values ('TEMP_INODE', 'DUMMY_OWNER', '1900-01-01 00:00:00.00', 'DUMMY_TYPE') " );
}
Structure fileAssetSt = StructureCache.getStructureByVelocityVarName("FileAsset");
// lets see if we have structures referencing the folder, if so, let's use its host for the dummy identifier
List<Structure> referencedStructures = APILocator.getFolderAPI().getStructures(folder, APILocator.getUserAPI().getSystemUser(), false);
String hostForDummyFolder = "SYSTEM_HOST";
if (referencedStructures!=null && !referencedStructures.isEmpty()) {
Structure st = referencedStructures.get(0);
hostForDummyFolder = st.getHost();
}
// 1.2) Insert dummy temp row on IDENTIFIER table
dc.executeStatement("insert into identifier values ('TEMP_IDENTIFIER', '/System folder', 'DUMMY_ASSET_NAME', '"+hostForDummyFolder+"', "
+ "'folder', NULL, NULL) ");
// 1.3) Insert dummy temp row on FOLDER table
if ( DbConnectionFactory.isOracle() ) {
dc.executeStatement("insert into folder values ('TEMP_INODE', 'DUMMY_NAME', 'DUMMY_TITLE', '"+DbConnectionFactory.getDBFalse()+"', '0', '', 'TEMP_IDENTIFIER', '"+ fileAssetSt.getInode()+ "', to_date('1900-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'))");
} else if(DbConnectionFactory.isPostgres()) {
dc.executeStatement("insert into folder values ('TEMP_INODE', 'DUMMY_NAME', 'DUMMY_TITLE', "+DbConnectionFactory.getDBFalse()+", '0', '', 'TEMP_IDENTIFIER', '"+ fileAssetSt.getInode()+ "', '1900-01-01 00:00:00.00')");
} else {
dc.executeStatement("insert into folder values ('TEMP_INODE', 'DUMMY_NAME', 'DUMMY_TITLE', '"+DbConnectionFactory.getDBFalse()+"', '0', '', 'TEMP_IDENTIFIER', '"+ fileAssetSt.getInode()+ "', '1900-01-01 00:00:00.00')");
}
// 2) Update references to the new dummies temps
// update foreign tables references to TEMP
dc.executeStatement("update structure set folder = 'TEMP_INODE' where folder = '"+oldFolderInode+"'");
dc.executeStatement("update permission set inode_id = 'TEMP_INODE' where inode_id = '"+oldFolderInode+"'");
dc.executeStatement("update permission_reference set asset_id = 'TEMP_INODE' where asset_id = '"+oldFolderInode+"'");
// 3.1) delete old FOLDER row
// lets save old folder columns values first
dc.setSQL("select * from folder where inode = ?");
dc.addParam(oldFolderInode);
Map<String, Object> oldFolderRow = dc.loadObjectResults().get(0);
String name = (String) oldFolderRow.get("name");
String title = (String) oldFolderRow.get("title");
Boolean showOnMenu = DbConnectionFactory.isDBTrue(oldFolderRow.get("show_on_menu").toString());
Integer sortOrder = 0;
if ( oldFolderRow.get( "sort_order" ) != null ) {
sortOrder = Integer.valueOf( oldFolderRow.get( "sort_order" ).toString() );
}
String filesMasks = (String) oldFolderRow.get("files_masks");
String defaultFileType = (String) oldFolderRow.get("default_file_type");
Date modDate = (Date) oldFolderRow.get("mod_date");
// lets save old identifier columns values first
dc.setSQL("select * from identifier where id = ?");
dc.addParam(oldFolderIdentifier);
Map<String, Object> oldIdentifierRow = dc.loadObjectResults().get(0);
String parentPath = (String) oldIdentifierRow.get("parent_path");
String assetName = (String) oldIdentifierRow.get("asset_name");
String hostId = (String) oldIdentifierRow.get("host_inode");
String assetType = (String) oldIdentifierRow.get("asset_type");
Date syspublishDate = (Date) oldIdentifierRow.get("syspublish_date");
Date sysexpireDate = (Date) oldIdentifierRow.get("sysexpire_date");
// now we can safely delete the old folder row. It will also delete the old Identifier
// lets alter the asset_name to avoid errors in validation when deleting the folder
dc.executeStatement("update identifier set asset_name = '_TO_BE_DELETED_' where id = '" + oldFolderIdentifier + "'");
dc.executeStatement("delete from folder where inode = '" + oldFolderInode + "'");
// 3.2) delete old INODE row
dc.setSQL("select * from inode where inode = ?");
dc.addParam(oldFolderInode);
Map<String, Object> oldInodeRow = dc.loadObjectResults().get(0);
String owner = (String) oldInodeRow.get("owner");
Date idate = (Date) oldInodeRow.get("idate");
String type = (String) oldInodeRow.get("type");
dc.executeStatement("delete from inode where inode = '" + oldFolderInode + "'");
// 4.1) insert real new INODE row
dc.setSQL("insert into inode values (?, ?, ?, ?) ");
dc.addParam(newFolderInode);
dc.addParam(owner);
dc.addParam(idate);
dc.addParam(type);
dc.loadResult();
// 4.2) insert real new IDENTIFIER row
dc.setSQL("insert into identifier values (?, ?, ?, ?, ?, ?, ?) ");
dc.addParam(newFolderIdentifier);
dc.addParam(parentPath);
dc.addParam(assetName);
dc.addParam(hostId);
dc.addParam(assetType);
dc.addParam(syspublishDate);
dc.addParam(sysexpireDate);
dc.loadResult();
// 4.3) insert real new FOLDER row
dc.setSQL("insert into folder values (?, ?, ?, ?, ?, ?, ?, ?, ?) ");
dc.addParam(newFolderInode);
dc.addParam(name);
dc.addParam(title);
dc.addParam(showOnMenu);
dc.addParam(sortOrder);
dc.addParam(filesMasks);
dc.addParam(newFolderIdentifier);
dc.addParam(defaultFileType);
dc.addParam(modDate);
dc.loadResult();
// 5) update foreign tables references to the new real row
dc.executeStatement("update structure set folder = '"+newFolderInode+"' where folder = 'TEMP_INODE'");
dc.executeStatement("update permission set inode_id = '"+newFolderInode+"' where inode_id = 'TEMP_INODE'");
dc.executeStatement("update permission_reference set asset_id = '"+newFolderInode+"' where asset_id = 'TEMP_INODE'");
// 6) delete dummy temp
dc.executeStatement("delete from folder where inode = 'TEMP_INODE'");
dc.executeStatement("delete from inode where inode = 'TEMP_INODE'");
} catch (DotDataException e) {
Logger.info(getClass(), "Folder not found. inode: " + oldFolderInode);
}
}
// lets reindex all the content under each fixed folder
dc.setSQL("select remote_inode from " + tableName + " where endpoint_id = ?");
dc.addParam(serverId);
results = dc.loadObjectResults();
for (Map<String, Object> result : results) {
final String newFolderInode = (String) result.get("remote_inode");
try {
HibernateUtil.addCommitListener(new Runnable() {
public void run() {
Folder folder = null;
try {
folder = APILocator.getFolderAPI().find(newFolderInode, APILocator.getUserAPI().getSystemUser(), false);
APILocator.getContentletAPI().refreshContentUnderFolder(folder);
} catch (DotStateException | DotDataException | DotSecurityException e) {
Logger.error(this,"Error while reindexing content under folder with inode: " + folder!=null?folder.getInode():"null",e);
}
}
});
} catch (DotDataException e) {