/* (non-Javadoc)
* @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
*/
@Override
public void execute(JobExecutionContext jobContext) throws JobExecutionException {
ContentletAPI contentletAPI = APILocator.getContentletAPI();
JobDataMap map = jobContext.getJobDetail().getJobDataMap();
Structure structure = (Structure) map.get("structure");
User user = (User) map.get("user");
try{
//Lucene query to be sure that I will get all fields of the contentlet
String luceneQuery = "+structureName:" + structure.getVelocityVarName() +
" +working:true" +
" +languageId:" + APILocator.getLanguageAPI().getDefaultLanguage().getId();
//Identifiers will be updated 500 at a time
Integer limit = 500;
Integer offset = 0;
//Get all the ContentletSearch
List<ContentletSearch> contenletSearchList = contentletAPI.searchIndex(luceneQuery, limit, offset, "random", user, false);
//If the query result is not empty
while(!contenletSearchList.isEmpty()){
//Start 500 (limit) transaction
HibernateUtil.startTransaction();
//Iterates all the ContentletSearch of the query
for(ContentletSearch contentletSearch : contenletSearchList){
//Get the identifier of each contentlet
Identifier identifier= APILocator.getIdentifierAPI().find(contentletSearch.getIdentifier());
//Gets from hibernate all the Data of the Contentlet
com.dotmarketing.portlets.contentlet.business.Contentlet fatty =
(com.dotmarketing.portlets.contentlet.business.Contentlet)HibernateUtil
.load(com.dotmarketing.portlets.contentlet.business.Contentlet.class, contentletSearch.getInode());
//Check if the new Publish Date Var is not null
if(UtilMethods.isSet(structure.getPublishDateVar())){
//Sets the identifier SysPublishDate to the new Structure/Content Publish Date Var
identifier.setSysPublishDate((Date)fatty.getMap().get(structure.getPublishDateVar()));
}else{
identifier.setSysPublishDate(null);
}
//Check if the new Expire Date Var is not null
if(UtilMethods.isSet(structure.getExpireDateVar())){
//Sets the identifier SysExpireDate to the new Structure/Content Expire Date Var
identifier.setSysExpireDate((Date)fatty.getMap().get(structure.getExpireDateVar()));
}else{
identifier.setSysExpireDate(null);
}
//Saves the update
APILocator.getIdentifierAPI().save(identifier);
//Clears Identifier Cache
CacheLocator.getIdentifierCache().removeFromCacheByIdentifier(contentletSearch.getIdentifier());
//Clears Contentlet Cache for each language and version
for(Language lan : APILocator.getLanguageAPI().getLanguages()) {
ContentletVersionInfo versionInfo = APILocator.getVersionableAPI().getContentletVersionInfo(identifier.getId(), lan.getId()) ;
if(versionInfo!=null && UtilMethods.isSet(versionInfo.getIdentifier())) {
CacheLocator.getContentletCache().remove(versionInfo.getWorkingInode());
if(UtilMethods.isSet(versionInfo.getLiveInode())) {
CacheLocator.getContentletCache().remove(versionInfo.getLiveInode());
}
}
}
}
//Commit 500 (limit) transaction
HibernateUtil.commitTransaction();
//Next 500
limit += limit;
offset += limit;
contenletSearchList = contentletAPI.searchIndex(luceneQuery, limit, offset, "random", user, false);
}
//Send Notification
String notificationMessage = LanguageUtil.get(user.getLocale(), "notifications_structure_identifiers_updated");
Notification n = new Notification(notificationMessage, NotificationLevel.INFO, user.getUserId());
APILocator.getNotificationAPI().saveNotification(n);