Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -952,38 +952,27 @@ protected List<Contentlet> findAllCurrent() throws DotDataException {
}

@Override
protected List<Contentlet> findAllCurrent (final int offset, final int limit ) throws ElasticsearchException {
protected List<Contentlet> findAllCurrent(final int offset, final int limit) throws DotDataException {

final String indexToHit;

try {
indexToHit = APILocator.getIndiciesAPI().loadIndicies().getWorking();
}
catch(DotDataException ee) {
Logger.fatal(this, "Can't get indicies information",ee);
return null;
}

final SearchRequest searchRequest = new SearchRequest(indexToHit);
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchSourceBuilder.size(limit);
searchSourceBuilder.from(offset);
searchSourceBuilder.timeout(TimeValue.timeValueMillis(INDEX_OPERATIONS_TIMEOUT_IN_MS));
searchSourceBuilder.fetchSource(new String[] {"inode"}, null);
searchRequest.source(searchSourceBuilder);

final SearchHits hits = cachedIndexSearch(searchRequest);

final int ultimateLimit = Math.min(limit, MAX_LIMIT);
final List<Contentlet> contentlets = new ArrayList<>();

for (final SearchHit hit : hits ) {
try {
final Map<String, Object> sourceMap = hit.getSourceAsMap();
contentlets.add( find( sourceMap.get("inode").toString()) );
} catch ( Exception e ) {
throw new ElasticsearchException( e.getMessage(), e );
try {
final DotConnect dotConnect = new DotConnect();
dotConnect.setSQL("SELECT working_inode FROM contentlet_version_info LIMIT ? OFFSET ?");
dotConnect.addParam(ultimateLimit);
dotConnect.addParam(offset);

final List<Map<String, Object>> results = dotConnect.loadObjectResults();
for (final Map<String, Object> result : results) {
final Contentlet contentlet = find((String) result.get("working_inode"));
// Filter out null entries - content may have been deleted but version info not yet cleaned up
if (contentlet != null) {
contentlets.add(contentlet);
}
}
} catch (Exception e) {
throw new DotDataException("Error finding all current contentlets", e);
}

return contentlets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,12 @@ public Object loadField(final String inode,
return contentFactory.loadField(inode, field.dbColumn());
}

/**
* @deprecated Do not use. For tests, use {@code ContentletDataGen.findAllContent(offset, limit)} instead.
*/
@CloseDBIfOpened
@Override
@Deprecated
public List<Contentlet> findAllContent(int offset, int limit) throws DotDataException {
return contentFactory.findAllCurrent(offset, limit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ public interface ContentletAPI {
String dnsRegEx = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$";

/**
* Use to retrieve all version of all content in the database. This is not a common method to use.
* Only use if you need to do maintenance tasks like search and replace something in every piece
* of content. Doesn't respect permissions.
* Retrieves contentlets from the database. Doesn't respect permissions.
*
* <p><strong>DO NOT USE THIS METHOD.</strong></p>
*
* <p>This method is deprecated and should not be used in production code as it may cause
* severe performance issues. For test code, use {@code ContentletDataGen.findAllContent(offset, limit)}
* from the test module instead, which provides the same functionality in a test-appropriate context.</p>
*
* @param offset can be 0 if no offset
* @param limit can be 0 of no limit
* @return List<Contentlet> list of contentlets
* @throws DotDataException
* @param limit can be 0 if no limit
* @return List of contentlets
* @throws DotDataException if a database error occurs
* @deprecated Do not use. For tests, use {@code ContentletDataGen.findAllContent(offset, limit)} instead.
*/
@Deprecated
public List<Contentlet> findAllContent(int offset, int limit) throws DotDataException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,11 @@ public List<Contentlet> find(List<Category> categories, long languageId, boolean
return c;
}

/**
* @deprecated Do not use. For tests, use {@code ContentletDataGen.findAllContent(offset, limit)} instead.
*/
@Override
@Deprecated
public List<Contentlet> findAllContent(int offset, int limit) throws DotDataException {
for(ContentletAPIPreHook pre : preHooks){
boolean preResult = pre.findAllContent(offset, limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ public interface ContentletAPIPostHook {

/**
* @param offset can be 0 if no offset
* @param limit can be 0 of no limit
* @param limit can be 0 if no limit
* @param returnValue - value returned by primary API Method
* @deprecated Do not use. For tests, use {@code ContentletDataGen.findAllContent(offset, limit)} instead.
*/
@Deprecated
public default void findAllContent(int offset, int limit, List<Contentlet> returnValue){}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public interface ContentletAPIPreHook {

/**
* @param offset can be 0 if no offset
* @param limit can be 0 of no limit
* @param limit can be 0 if no limit
* @return false if the hook should stop the transaction
* @deprecated Do not use. For tests, use {@code ContentletDataGen.findAllContent(offset, limit)} instead.
*/
@Deprecated
public default boolean findAllContent(int offset, int limit){
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ public void test_indexContentList_with_diff_refresh_strategies() throws Exceptio

generateTestContentlets();

final List<Contentlet> contentlets = contentletAPI.findAllContent(0, 100)
.stream().filter(Objects::nonNull).collect(Collectors.toList());
final List<Contentlet> contentlets = ContentletDataGen.findAllContent(0, 100);

assertNotNull(contentlets);
assertTrue("The number of contentlet returned is: " + contentlets.size(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public static Contentlet publish(Contentlet contentlet) {
@WrapInTransaction
public static void archive(Contentlet contentlet) {
try{
contentlet.setIndexPolicy(IndexPolicy.WAIT_FOR);
contentletAPI.archive(contentlet, APILocator.systemUser(), false);
} catch (DotContentletStateException | DotDataException | DotSecurityException e) {
throw new RuntimeException(e);
Expand All @@ -377,6 +378,7 @@ public static void archive(Contentlet contentlet) {
@WrapInTransaction
public static void delete(Contentlet contentlet) {
try{
contentlet.setIndexPolicy(IndexPolicy.WAIT_FOR);
contentletAPI.delete(contentlet, APILocator.systemUser(), false);
} catch (DotContentletStateException | DotDataException | DotSecurityException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -405,6 +407,7 @@ public static void destroy(final Contentlet contentlet, final Boolean failSilent

if (null != contentlet) {
try {
contentlet.setIndexPolicy(IndexPolicy.WAIT_FOR);
APILocator.getContentletAPI().destroy(contentlet, APILocator.systemUser(), false);
} catch (Exception e) {
if (failSilently) {
Expand Down Expand Up @@ -505,4 +508,36 @@ public static void update(final Contentlet contentlet, final Map<String, Object>
}

}

/**
* Retrieves contentlets from the database for test purposes.
* This method queries the contentlet_version_info table directly to get working contentlet inodes,
* which is more reliable than using ES index (which may have stale entries after deletions).
*
* @param offset the starting position (0-based)
* @param limit the maximum number of contentlets to return
* @return list of contentlets (never null, may be empty)
* @throws DotDataException if a database error occurs
*/
public static List<Contentlet> findAllContent(final int offset, final int limit) throws DotDataException {
final List<Contentlet> contentlets = new ArrayList<>();
try {
final com.dotmarketing.common.db.DotConnect dotConnect = new com.dotmarketing.common.db.DotConnect();
dotConnect.setSQL("SELECT working_inode FROM contentlet_version_info LIMIT ? OFFSET ?");
dotConnect.addParam(limit);
dotConnect.addParam(offset);

final List<Map<String, Object>> results = dotConnect.loadObjectResults();
for (final Map<String, Object> result : results) {
final Contentlet contentlet = contentletAPI.find(
(String) result.get("working_inode"), APILocator.systemUser(), false);
if (contentlet != null) {
contentlets.add(contentlet);
}
}
} catch (DotSecurityException e) {
throw new DotDataException("Error finding all content", e);
}
return contentlets;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dotcms.IntegrationTestBase;
import com.dotcms.LicenseTestUtil;
import com.dotcms.datagen.ContentletDataGen;
import com.dotcms.publisher.assets.bean.PushedAsset;
import com.dotcms.publisher.bundle.bean.Bundle;
import com.dotcms.publisher.bundle.business.BundleAPI;
Expand Down Expand Up @@ -130,8 +131,7 @@ private List<PushedAsset> getPushedAssets(final PushedAssetsAPI pushedAssetsAPI,
final Bundle bundle1,
final User adminUser) throws DotDataException {
PushedAsset pushedAsset = null;
final List<Contentlet> contentlets = contentletAPI.findAllContent
( 0, 30 ).stream().filter(Objects::nonNull).collect(Collectors.toList());
final List<Contentlet> contentlets = ContentletDataGen.findAllContent(0, 30);
final List<PushedAsset> pushedAssets = new ArrayList<>();
final Date pushDate = new Date();
for (final Contentlet contentlet : contentlets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public void testGet_showCategories_AsAnonUser() throws DotDataException, DotSecu

APILocator.getPermissionAPI().save(catsPermsSystemHost, APILocator.systemHost(),
APILocator.systemUser(), false);
// Ensure permissions are properly propagated to child categories
APILocator.getPermissionAPI().resetPermissionsUnder(APILocator.systemHost());

//Create Categories
final Category categoryChild1 = new CategoryDataGen().setCategoryName("RoadBike-"+System.currentTimeMillis()).setKey("RoadBike").setKeywords("RoadBike").setCategoryVelocityVarName("roadBike").next();
Expand Down Expand Up @@ -475,7 +477,7 @@ private Field createKeyValueField(final String fieldName, final String contentTy
@Test
public void testGetRecycledRequest() throws DotDataException, DotSecurityException {

final List<Contentlet> contentlets = APILocator.getContentletAPI().findAllContent(1, 5);
final List<Contentlet> contentlets = ContentletDataGen.findAllContent(1, 5);
if(contentlets.isEmpty()) {
throw new DotDataException("No contentlets found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,19 +635,19 @@ public void testCheckinNoDefaultActions() throws DotDataException, DotSecurityEx
}

/**
* Testing {@link ContentletAPI#findAllContent(int, int)}
* Testing {@link ContentletDataGen#findAllContent(int, int)}
*
* @throws com.dotmarketing.exception.DotDataException
* @throws com.dotmarketing.exception.DotSecurityException
* @see ContentletAPI
* @see ContentletDataGen
* @see Contentlet
*/
@Ignore("Not Ready to Run.")
@Test
public void findAllContent() throws DotDataException, DotSecurityException {

//Getting all contentlets live/working contentlets
List<Contentlet> contentlets = contentletAPI.findAllContent(0, 5);
List<Contentlet> contentlets = ContentletDataGen.findAllContent(0, 5);

//Validations
assertTrue(contentlets != null && !contentlets.isEmpty());
Expand Down