List<String> whereToSend = Arrays.asList(whoToSendTmp.split(","));
List<Environment> envsToSendTo = new ArrayList<Environment>();
// Lists of Environments to push to
for (String envId : whereToSend) {
Environment e = APILocator.getEnvironmentAPI().findEnvironmentById(envId);
if(e!=null) {
envsToSendTo.add(e);
}
}
//Put the selected environments in session in order to have the list of the last selected environments
request.getSession().setAttribute( WebKeys.SELECTED_ENVIRONMENTS + getUser().getUserId(), envsToSendTo );
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd-H-m" );
Date publishDate = dateFormat.parse( _contentPushPublishDate + "-" + _contentPushPublishTime );
List<String> ids;
if ( _assetId.startsWith( "query_" ) ) { //Support for lucene queries
String luceneQuery = _assetId.replace( "query_", "" );
List<String> queries = new ArrayList<String>();
queries.add( luceneQuery );
ids = PublisherUtil.getContentIds( queries );
} else {
String[] _assetsIds = _assetId.split( "," );//Support for multiple ids in the assetIdentifier parameter
List<String> assetsIds = Arrays.asList( _assetsIds );
ids = getIdsToPush( assetsIds, null, _contentFilterDate, dateFormat );
}
//Response map with the status of the addContents operation (error messages and counts )
Map<String, Object> responseMap = null;
if ( _iWantTo.equals( RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH ) || _iWantTo.equals( RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH_AND_EXPIRE ) ) {
Bundle bundle = new Bundle(null, publishDate, null, getUser().getUserId(), forcePush);
APILocator.getBundleAPI().saveBundle(bundle, envsToSendTo);
responseMap = publisherAPI.addContentsToPublish( ids, bundle.getId(), publishDate, getUser() );
}
if ( _iWantTo.equals( RemotePublishAjaxAction.DIALOG_ACTION_EXPIRE ) || _iWantTo.equals( RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH_AND_EXPIRE ) ) {
if ( (!"".equals( _contentPushExpireDate.trim() ) && !"".equals( _contentPushExpireTime.trim() )) ) {
Date expireDate = dateFormat.parse( _contentPushExpireDate + "-" + _contentPushExpireTime );
Bundle bundle = new Bundle(null, publishDate, expireDate, getUser().getUserId(), forcePush);
APILocator.getBundleAPI().saveBundle(bundle, envsToSendTo);
responseMap = publisherAPI.addContentsToUnpublish( ids, bundle.getId(), expireDate, getUser() );
}
}
//If we have errors lets return them in order to feedback the user
if ( responseMap != null && !responseMap.isEmpty() ) {
//Error messages
JSONArray jsonErrors = new JSONArray( (ArrayList) responseMap.get( "errorMessages" ) );
//Prepare the Json response
JSONObject jsonResponse = new JSONObject();
jsonResponse.put( "errorMessages", jsonErrors.toArray() );
jsonResponse.put( "errors", responseMap.get( "errors" ) );
jsonResponse.put( "total", responseMap.get( "total" ) );
jsonResponse.put( "bundleId", responseMap.get( "bundleId" ) );
//And send it back to the user
response.getWriter().println( jsonResponse.toString() );
}
} catch ( Exception e ) {
Logger.error( RemotePublishAjaxAction.class, e.getMessage(), e );
response.sendError( HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error Publishing Bundle: " + e.getMessage() );
}
}