Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Created October 11, 2016 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfalzone/5d5e584afc668b0885492dac628bbaf5 to your computer and use it in GitHub Desktop.
Save cfalzone/5d5e584afc668b0885492dac628bbaf5 to your computer and use it in GitHub Desktop.
Just some methods to push content in dotCMS
public final class Pushing {
public static final Pushing INSTANCE = new Pushing();
private User sysUser = null;
private EnvironmentAPI envAPI = APILocator.getEnvironmentAPI();
private BundleAPI bundleAPI = APILocator.getBundleAPI();
private PublisherAPI pubAPI = PublisherAPI.getInstance();
private UserAPI userAPI = APILocator.getUserAPI();
private Pushing() { }
/**
* Get an instance of the util class.
*
* @return an instance of the util class
*/
public static Pushing getInstance() {
return INSTANCE;
}
/**
* Get the system user.
*
* @return The system user
*/
public User getSystemUser() {
if (sysUser == null) {
try {
sysUser = userAPI.getSystemUser();
} catch (Exception e) {
Logger.error(this, "Cound not get the system user", e);
}
}
return sysUser;
}
/**
* Pushes a list of identifiers to the default sync environment.
*
* @param ids The identifiers to push
* @throws Exception when the environment is null.
*/
public void pushSync(List<String> ids) throws Exception {
// The name of the environment you are pusing to
String env = "Dev + Prod";
pushIds(ids, env);
}
/**
* Pushes a contentlet to the default sync environment.
*
* @param con The contentlet to push
* @throws Exception when the environment is null
*/
public void pushSync(Contentlet con) throws Exception {
// The name of the environment you are pusing to
String env = "Dev + Prod";
if (env == null) {
throw new Exception("Unable to determine push environment");
}
pushContentlet(con, env);
}
/**
* Pushes a contentlet to the specified environment.
*
* @param con The contentlet to push
* @param env The environment to push to
* @throws Exception from pushIds
*/
public void pushContentlet(Contentlet con, String env) throws Exception {
// Make a list of items to push
List<String> ids = new ArrayList<String>();
ids.add(con.getIdentifier());
pushIds(ids, env);
}
/**
* Pushes a list of identifiers to the specified environment.
*
* @param ids The identifiers to push
* @param env The environment to push to
* @throws Exception when it cannot get the push environment
*/
public void pushIds(List<String> ids, String env) throws Exception {
// Make a list of environments to push to
// You can add more than one here if you want to push to multiple environments
// We made a single environment named "Dev + Prod" that includes the dev and both of the Production servers
List<Environment> envs = new ArrayList<Environment>();
try {
envs.add(envAPI.findEnvironmentByName(env));
} catch (Exception e) {
Logger.error(this, "Exception trying to get push environment: " + env, e);
throw new Exception(e);
}
// Push the Content
Map<String, Object> responseMap = null;
try {
// Date to push the content
Date publishDate = new Date();
// Create a new bundle to push
Bundle bundle = new Bundle(null, publishDate, null, getSystemUser().getUserId(), false);
bundleAPI.saveBundle(bundle, envs);
// Push the bundle
// Note that this only queues the bundle to be pushed
responseMap = pubAPI.addContentsToPublish(ids, bundle.getId(), publishDate, getSystemUser());
} catch (Exception e) {
Logger.error(this, "Error pushing contentlets", e);
throw new Exception(e);
}
// Check the response
if (responseMap != null && !responseMap.isEmpty()) {
int errors = (Integer) responseMap.get("errors");
int total = (Integer) responseMap.get("total");
Logger.debug(this, "Push Reponse is " + responseMap);
if (errors == 0 && total >= 1) {
// If there are no errors and at least one thing was successful then it is good
// At this point the bundle is queued to push
// We don't know here if the push was successful, only the queueing
return;
} else {
// Probably could do a little more with the errors here
Logger.error(this, "Invalid Response pushing content. errors=" + errors + ", total=" + total);
throw new Exception("Invalid Response pushing content.");
}
} else {
Logger.error(this, "No Response pushing content");
throw new Exception("No Response pushing content");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment