Skip to content

Instantly share code, notes, and snippets.

@nkeiter
Created November 6, 2017 20:21
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 nkeiter/604d27e17c492a826079a71ba7746a05 to your computer and use it in GitHub Desktop.
Save nkeiter/604d27e17c492a826079a71ba7746a05 to your computer and use it in GitHub Desktop.
package edu.gettysburg.nkeiter.transportation.rest.utilities;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.UserAPI;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.structure.model.Relationship;
import com.liferay.portal.model.User;
import edu.gettysburg.nkeiter.transportation.rest.log.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ContentletUtility
{
public static final Class<ContentletUtility> clazz = ContentletUtility.class;
public ContentletUtility()
{
// do nothing
}
public static Contentlet checkoutContentlet( Contentlet contentlet )
{
Contentlet checkedOutContentlet = null;
try
{
checkedOutContentlet = ContentletUtility.checkoutContentlet( contentlet, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "checkoutContentlet( Contentlet )", exception );
}
return checkedOutContentlet;
}
public static Contentlet checkoutContentlet( Contentlet contentlet, User user )
{
Contentlet checkedOutContentlet = null;
try
{
checkedOutContentlet = ContentletUtility.checkoutContentlet( contentlet, user, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "checkoutContentlet( Contentlet, User user )", exception );
}
return checkedOutContentlet;
}
public static Contentlet checkoutContentlet( Contentlet contentlet, User user, boolean respectFrontendRoles )
{
Contentlet checkedOutContentlet = null;
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
if ( user == null )
{
user = systemUser;
}
checkedOutContentlet = contentletAPI.checkout( contentlet.getInode(), user, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "checkoutContentlet( Contentlet, User user, boolean )", exception );
}
return checkedOutContentlet;
}
public static List<Contentlet> getRelatedContentlets( Contentlet contentlet, Relationship relationship )
{
List<Contentlet> relatedContentletsList = new ArrayList<Contentlet>();
try
{
relatedContentletsList = getRelatedContentlets( contentlet, relationship, null, true, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "getRelatedContentlets( Contentlet, Relationship )", exception );
}
return relatedContentletsList;
}
public static List<Contentlet> getRelatedContentlets( Contentlet contentlet, Relationship relationship, boolean persist )
{
List<Contentlet> relatedContentletsList = new ArrayList<Contentlet>();
try
{
relatedContentletsList = getRelatedContentlets( contentlet, relationship, null, true, persist );
}
catch ( Exception exception )
{
Logger.error( clazz, "getRelatedContentlets( Contentlet, Relationship )", exception );
}
return relatedContentletsList;
}
public static List<Contentlet> getRelatedContentlets( Contentlet contentlet, Relationship relationship, User user )
{
List<Contentlet> relatedContentletsList = new ArrayList<Contentlet>();
try
{
relatedContentletsList = getRelatedContentlets( contentlet, relationship, user, true, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "getRelatedContentlets( Contentlet, Relationship, User )", exception );
}
return relatedContentletsList;
}
public static List<Contentlet> getRelatedContentlets( Contentlet contentlet, Relationship relationship, User user, boolean persist )
{
List<Contentlet> relatedContentletsList = new ArrayList<Contentlet>();
try
{
relatedContentletsList = getRelatedContentlets( contentlet, relationship, user, true, persist );
}
catch ( Exception exception )
{
Logger.error( clazz, "getRelatedContentlets( Contentlet, Relationship, User )", exception );
}
return relatedContentletsList;
}
public static List<Contentlet> getRelatedContentlets( Contentlet contentlet, Relationship relationship, User user, boolean respectFrontendRoles, boolean persist )
{
List<Contentlet> relatedContentletsList = new ArrayList<Contentlet>();
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
int tryCount = 0;
if ( user == null )
{
user = systemUser;
}
relatedContentletsList = contentletAPI.getRelatedContent( contentlet, relationship, user, respectFrontendRoles );
tryCount++;
if ( persist )
{
// Wait one second for related content indexing processes.
Thread.sleep( 1000 );
while ( relatedContentletsList.size() == 0 && tryCount < 10000 )
{
if ( contentletAPI.isInodeIndexed( contentlet.getInode() ) )
{
relatedContentletsList = contentletAPI.getRelatedContent( contentlet, relationship, user, respectFrontendRoles );
tryCount++;
}
}
}
Logger.info( clazz, "persist = " + persist );
Logger.info( clazz, "contentlet.getIdentifier() = " + contentlet.getIdentifier() );
Logger.info( clazz, "contentlet.getInode() = " + contentlet.getInode() );
Logger.info( clazz, "relationship.getRelationTypeValue() = " + relationship.getRelationTypeValue() );
Logger.info( clazz, "relatedContentletsList.size() = " + relatedContentletsList.size() );
Logger.info( clazz, "tryCount = " + tryCount );
}
catch ( Exception exception )
{
Logger.error( clazz, "getRelatedContentlets( Contentlet, Relationship, User, boolean )", exception );
}
return relatedContentletsList;
}
public static List<Contentlet> findContentlet( String structureName, List<String> conditions )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentlet( structureName, conditions, 1, null, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentlet( String, List<String> )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentlet( String structureName, List<String> conditions, String sortBy )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentlet( structureName, conditions, 1, sortBy, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentlet( String, List<String>, String )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentlet( String structureName, List<String> conditions, int limit )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentlet( structureName, conditions, limit, null, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentlet( String, List<String>, int, String )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentlet( String structureName, List<String> conditions, int limit, String sortBy )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentlet( structureName, conditions, limit, sortBy, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentlet( String, List<String>, int, String )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentlet( String structureName, List<String> conditions, int limit, String sortBy, User user )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentlet( structureName, conditions, limit, sortBy, user, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentlet( String, List<String>, int, String, User )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentlet( String structureName, List<String> conditions, int limit, String sortBy, User user, boolean respectFrontendRoles )
{
List<Contentlet> contentletList = null;
String luceneQuery = "";
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
if ( user == null )
{
user = systemUser;
}
luceneQuery = "+structureName: " + structureName;
if ( conditions != null && conditions.size() > 0 )
{
for ( String condition : conditions )
{
luceneQuery = luceneQuery + " " + condition;
}
}
luceneQuery = luceneQuery + " +deleted:false +working:true";
contentletList = contentletAPI.search( luceneQuery, limit, -1, sortBy, user, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentlet( String, List<String>, int, String, User, boolean ) luceneQuery: " + luceneQuery, exception );
}
return contentletList;
}
public static List<Contentlet> findContentletAnonymous( String structureName, List<String> conditions )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentletAnonymous( structureName, conditions, 1, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletAnonymous( String, List<String> )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentletAnonymous( String structureName, List<String> conditions, String sortBy )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentletAnonymous( structureName, conditions, 1, sortBy, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletAnonymous( String, List<String>, String )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentletAnonymous( String structureName, List<String> conditions, int limit )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentletAnonymous( structureName, conditions, limit, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletAnonymous( String, List<String>, int, String )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentletAnonymous( String structureName, List<String> conditions, int limit, String sortBy )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentletAnonymous( structureName, conditions, limit, sortBy, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletAnonymous( String, List<String>, int, String )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentletAnonymous( String structureName, List<String> conditions, int limit, String sortBy, boolean respectFrontendRoles )
{
List<Contentlet> contentletList = null;
String luceneQuery = "";
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User anonymousUser = userAPI.getAnonymousUser();
luceneQuery = "+structureName: " + structureName;
if ( conditions != null && conditions.size() > 0 )
{
for ( String condition : conditions )
{
luceneQuery = luceneQuery + " " + condition;
}
}
luceneQuery = luceneQuery + " +deleted:false +working:true";
contentletList = contentletAPI.search( luceneQuery, limit, -1, sortBy, anonymousUser, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletAnonymous( String, List<String>, int, String, boolean ) luceneQuery: " + luceneQuery, exception );
}
return contentletList;
}
public static Contentlet findContentletByIdentifier( String identifier )
{
Contentlet contentlet = null;
try
{
contentlet = ContentletUtility.findContentletByIdentifier( identifier, true, 1, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletByIdentifier( String )", exception );
}
return contentlet;
}
public static Contentlet findContentletByIdentifier( String identifier, boolean live )
{
Contentlet contentlet = null;
try
{
contentlet = ContentletUtility.findContentletByIdentifier( identifier, live, 1, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletByIdentifier( String, boolean )", exception );
}
return contentlet;
}
public static Contentlet findContentletByIdentifier( String identifier, boolean live, long languageId )
{
Contentlet contentlet = null;
try
{
contentlet = ContentletUtility.findContentletByIdentifier( identifier, live, languageId, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletByIdentifier( String, boolean, long )", exception );
}
return contentlet;
}
public static Contentlet findContentletByIdentifier( String identifier, boolean live, long languageId, User user )
{
Contentlet contentlet = null;
try
{
contentlet = ContentletUtility.findContentletByIdentifier( identifier, live, languageId, user, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletByIdentifier( String, boolean, long, User )", exception );
}
return contentlet;
}
public static Contentlet findContentletByIdentifier( String identifier, boolean live, long languageId, User user, boolean respectFrontendRoles )
{
Contentlet contentlet = null;
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
if ( user == null )
{
user = systemUser;
}
contentlet = contentletAPI.findContentletByIdentifier( identifier, live, languageId, user, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletByIdentifier( String, boolean, long, User, boolean )", exception );
}
return contentlet;
}
public static List<Contentlet> findContentletSystem( String structureName, List<String> conditions )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentletSystem( structureName, conditions, 1, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletSystem( String, List<String> )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentletSystem( String structureName, List<String> conditions, String sortBy )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentletSystem( structureName, conditions, 1, sortBy, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletSystem( String, List<String>, int, String, boolean )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentletSystem( String structureName, List<String> conditions, int limit )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentletSystem( structureName, conditions, limit, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletSystem( String, List<String>, int, String, boolean )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentletSystem( String structureName, List<String> conditions, int limit, String sortBy )
{
List<Contentlet> contentletList = null;
try
{
contentletList = ContentletUtility.findContentletSystem( structureName, conditions, limit, sortBy, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletSystem( String, List<String>, int, String, boolean )", exception );
}
return contentletList;
}
public static List<Contentlet> findContentletSystem( String structureName, List<String> conditions, int limit, String sortBy, boolean respectFrontendRoles )
{
List<Contentlet> contentletList = null;
String luceneQuery = "";
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
luceneQuery = "+structureName: " + structureName;
if ( conditions != null && conditions.size() > 0 )
{
for ( String condition : conditions )
{
luceneQuery = luceneQuery + " " + condition;
}
}
luceneQuery = luceneQuery + " +deleted:false +working:true";
contentletList = contentletAPI.search( luceneQuery, limit, -1, sortBy, systemUser, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "ContentletUtility.findContentletSystem( String, List<String>, int, String, boolean ) luceneQuery: " + luceneQuery, exception );
}
return contentletList;
}
public static Contentlet publishContentlet( Contentlet contentlet )
{
Contentlet publishedContentlet = null;
try
{
publishedContentlet = ContentletUtility.publishContentlet( contentlet, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "publishContentlet( Contentlet )", exception );
}
return publishedContentlet;
}
public static Contentlet publishContentlet( Contentlet contentlet, User user )
{
Contentlet publishedContentlet = null;
try
{
publishedContentlet = ContentletUtility.publishContentlet( contentlet, user, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "publishContentlet( Contentlet, User user )", exception );
}
return publishedContentlet;
}
public static Contentlet publishContentlet( Contentlet contentlet, User user, boolean respectFrontendRoles )
{
Contentlet publishedContentlet = null;
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
if ( user == null )
{
user = systemUser;
}
publishedContentlet = contentletAPI.checkin( contentlet, user, respectFrontendRoles );
contentletAPI.publish( publishedContentlet, systemUser, respectFrontendRoles );
contentletAPI.unlock( publishedContentlet, systemUser, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "publishContentlet( Contentlet, User user, boolean )", exception );
}
return publishedContentlet;
}
public static Contentlet publishContentletWithRelationships( Contentlet contentlet, Map<Relationship, List<Contentlet>> contentletRelationships )
{
Contentlet publishedContentlet = null;
try
{
publishedContentlet = ContentletUtility.publishContentletWithRelationships( contentlet, contentletRelationships, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "publishContentletWithRelationships( Contentlet, Map<Relationship, List<Contentlet>> )", exception );
}
return publishedContentlet;
}
public static Contentlet publishContentletWithRelationships( Contentlet contentlet, Map<Relationship, List<Contentlet>> contentletRelationships, User user )
{
Contentlet publishedContentlet = null;
try
{
publishedContentlet = ContentletUtility.publishContentletWithRelationships( contentlet, contentletRelationships, user, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "publishContentletWithRelationships( Contentlet, Map<Relationship, List<Contentlet>>, User user )", exception );
}
return publishedContentlet;
}
public static Contentlet publishContentletWithRelationships( Contentlet contentlet, Map<Relationship, List<Contentlet>> contentletRelationships, User user, boolean respectFrontendRoles )
{
Contentlet publishedContentlet = null;
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
if ( user == null )
{
user = systemUser;
}
publishedContentlet = contentletAPI.checkin( contentlet, contentletRelationships, user, respectFrontendRoles );
contentletAPI.publish( publishedContentlet, systemUser, respectFrontendRoles );
contentletAPI.unlock( publishedContentlet, systemUser, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "publishContentletWithRelationships( Contentlet, Map<Relationship, List<Contentlet>>, User, boolean )", exception );
}
return publishedContentlet;
}
public static Contentlet saveContentlet( Contentlet contentlet )
{
Contentlet savedContentlet = null;
try
{
savedContentlet = ContentletUtility.saveContentlet( contentlet, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "saveContentlet( Contentlet )", exception );
}
return savedContentlet;
}
public static Contentlet saveContentlet( Contentlet contentlet, User user )
{
Contentlet savedContentlet = null;
try
{
savedContentlet = ContentletUtility.saveContentlet( contentlet, user, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "saveContentlet( Contentlet, User user )", exception );
}
return savedContentlet;
}
public static Contentlet saveContentlet( Contentlet contentlet, User user, boolean respectFrontendRoles )
{
Contentlet savedContentlet = null;
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
if ( user == null )
{
user = systemUser;
}
savedContentlet = contentletAPI.checkin( contentlet, user, respectFrontendRoles );
contentletAPI.unlock( savedContentlet, systemUser, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "saveContentlet( Contentlet, User user, boolean )", exception );
}
return savedContentlet;
}
public static Contentlet saveContentletWithRelationships( Contentlet contentlet, Map<Relationship, List<Contentlet>> contentletRelationships )
{
Contentlet savedContentlet = null;
try
{
savedContentlet = ContentletUtility.saveContentletWithRelationships( contentlet, contentletRelationships, null, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "saveContentletWithRelationships( Contentlet, Map<Relationship, List<Contentlet>> )", exception );
}
return savedContentlet;
}
public static Contentlet saveContentletWithRelationships( Contentlet contentlet, Map<Relationship, List<Contentlet>> contentletRelationships, User user )
{
Contentlet savedContentlet = null;
try
{
savedContentlet = ContentletUtility.saveContentletWithRelationships( contentlet, contentletRelationships, user, true );
}
catch ( Exception exception )
{
Logger.error( clazz, "saveContentletWithRelationships( Contentlet, Map<Relationship, List<Contentlet>>, User user )", exception );
}
return savedContentlet;
}
public static Contentlet saveContentletWithRelationships( Contentlet contentlet, Map<Relationship, List<Contentlet>> contentletRelationships, User user, boolean respectFrontendRoles )
{
Contentlet savedContentlet = null;
try
{
// dotCMS API's
ContentletAPI contentletAPI = APILocator.getContentletAPI();
UserAPI userAPI = APILocator.getUserAPI();
User systemUser = userAPI.getSystemUser();
if ( user == null )
{
user = systemUser;
}
savedContentlet = contentletAPI.checkin( contentlet, contentletRelationships, user, respectFrontendRoles );
contentletAPI.unlock( savedContentlet, systemUser, respectFrontendRoles );
}
catch ( Exception exception )
{
Logger.error( clazz, "saveContentletWithRelationships( Contentlet, Map<Relationship, List<Contentlet>>, User, boolean )", exception );
}
return savedContentlet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment