Skip to content

Commit

Permalink
#24738 improve finding all versions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizzio-dotCMS committed Jun 29, 2023
1 parent 970a508 commit c5a888a
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 34 deletions.
16 changes: 9 additions & 7 deletions dotCMS/src/curl-test/WebAssets.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@
}
]
},
"method": "DELETE",
"method": "POST",
"header": [],
"body": {
"mode": "raw",
Expand All @@ -871,14 +871,15 @@
}
},
"url": {
"raw": "{{serverURL}}/api/v1/assets",
"raw": "{{serverURL}}/api/v1/assets/_delete",
"host": [
"{{serverURL}}"
],
"path": [
"api",
"v1",
"assets"
"assets",
"_delete"
]
},
"description": "Simple delete file test"
Expand Down Expand Up @@ -916,7 +917,7 @@
}
]
},
"method": "DELETE",
"method": "POST",
"header": [],
"body": {
"mode": "raw",
Expand All @@ -928,15 +929,16 @@
}
},
"url": {
"raw": "{{serverURL}}/api/v1/assets/folder",
"raw": "{{serverURL}}/api/v1/assets/folders/_delete",
"host": [
"{{serverURL}}"
],
"path": [
"api",
"v1",
"assets",
"folder"
"folders",
"_delete"
]
},
"description": "Delete folder"
Expand Down Expand Up @@ -974,7 +976,7 @@
}
]
},
"method": "DELETE",
"method": "POST",
"header": [],
"body": {
"mode": "raw",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1426,4 +1426,75 @@ public void findAllVersionsWithOldVersionsByVariant() throws DotDataException, D

}

/**
* Testing the method {@link ESContentFactoryImpl#findAllVersions(Identifier, boolean)}
* This version of the method takes a collection of identifiers and returns all the versions of the contentlets
* Given scenario: The contentlet had several versions in different {@link Language} `
* Expected result: The method should return all the versions of the contentlets
* @throws DotDataException
* @throws DotSecurityException
*/
@Test
public void TestFindAllVersions() throws DotDataException, DotSecurityException {

final Language language_1 = new com.dotcms.datagen.LanguageDataGen().nextPersisted();
final Language language_2 = new com.dotcms.datagen.LanguageDataGen().nextPersisted();
final Language language_3 = new com.dotcms.datagen.LanguageDataGen().nextPersisted();

final Host host = new SiteDataGen().nextPersisted();

final ContentType contentType = new ContentTypeDataGen().nextPersisted();
final Contentlet contentletLanguage1Live = new ContentletDataGen(contentType)
.languageId(language_1.getId())
.host(host)
.nextPersistedAndPublish();

Contentlet contentlet1Checkout = ContentletDataGen.checkout(contentletLanguage1Live);
final Contentlet contentletLanguage1Working = ContentletDataGen.checkin(contentlet1Checkout);

final Contentlet contentletLanguage2Live = createNewLangVersionAndPublish(language_2, contentletLanguage1Live);

final Contentlet contentlet2Checkout = ContentletDataGen.checkout(contentletLanguage2Live);
final Contentlet contentletLanguage2Working = ContentletDataGen.checkin(contentlet2Checkout);

final Contentlet contentletLanguage3Live = createNewLangVersionAndPublish(language_3, contentletLanguage1Live);
final Contentlet contentlet3Checkout = ContentletDataGen.checkout(contentletLanguage3Live);
final Contentlet contentletLanguage3Working = ContentletDataGen.checkin(contentlet3Checkout);

final Contentlet contentletLanguage1Live2 = new ContentletDataGen(contentType)
.languageId(language_1.getId())
.host(host)
.nextPersistedAndPublish();

final Contentlet contentletLanguage1Live3 = new ContentletDataGen(contentType)
.languageId(language_1.getId())
.host(host)
.nextPersistedAndPublish();

final Contentlet contentletLanguage1Live4 = new ContentletDataGen(contentType)
.languageId(language_1.getId())
.host(host)
.nextPersistedAndPublish();

final List<Contentlet> contentlets = List.of(contentletLanguage1Live,
contentletLanguage1Working, contentletLanguage2Live, contentletLanguage2Working,
contentletLanguage3Live, contentletLanguage3Working, contentletLanguage1Live2,
contentletLanguage1Live3,
contentletLanguage1Live4);

final Set<String> identifiers = contentlets.stream().map(Contentlet::getIdentifier)
.collect(Collectors.toSet());

ESContentFactoryImpl impl = (ESContentFactoryImpl) FactoryLocator.getContentletFactory();
final List<Contentlet> allVersions = impl.findAllVersions(identifiers);

Assert.assertEquals(9, allVersions.size());

for (Contentlet c:contentlets) {
Assert.assertTrue(allVersions.stream().anyMatch(contentlet -> contentlet.getIdentifier().equals(c.getIdentifier())));
Assert.assertTrue(allVersions.stream().anyMatch(contentlet -> contentlet.getInode().equals(c.getInode())));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import com.rainerhahnekamp.sneakythrow.Sneaky;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -109,6 +110,7 @@
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.settings.Settings;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -2222,6 +2224,80 @@ private static Contentlet createNewLangVersion(final Language language,
return ContentletDataGen.checkin(contentlet1Checkout);
}

/**
* Testing the method {@link ContentletAPI#findAllVersions(Set, User, boolean)}
* This version of the method takes a collection of identifiers and returns all the versions of the contentlets
* Given scenario: The contentlet had several versions in different {@link Language} `
* Expected result: The method should return all the versions of the contentlets
* @throws DotDataException
* @throws DotSecurityException
*/
@Test
public void TestFindAllVersions() throws DotDataException, DotSecurityException {

final Language language_1 = new com.dotcms.datagen.LanguageDataGen().nextPersisted();
final Language language_2 = new com.dotcms.datagen.LanguageDataGen().nextPersisted();
final Language language_3 = new com.dotcms.datagen.LanguageDataGen().nextPersisted();

final Host host = new SiteDataGen().nextPersisted();

final ContentType contentType = new ContentTypeDataGen().nextPersisted();
final Contentlet contentletLanguage1Live = new ContentletDataGen(contentType)
.languageId(language_1.getId())
.host(host)
.nextPersistedAndPublish();

Contentlet contentlet1Checkout = ContentletDataGen.checkout(contentletLanguage1Live);
final Contentlet contentletLanguage1Working = ContentletDataGen.checkin(contentlet1Checkout);

final Contentlet contentletLanguage2Live = createNewLangVersionAndPublish(language_2, contentletLanguage1Live);

final Contentlet contentlet2Checkout = ContentletDataGen.checkout(contentletLanguage2Live);
final Contentlet contentletLanguage2Working = ContentletDataGen.checkin(contentlet2Checkout);

final Contentlet contentletLanguage3Live = createNewLangVersionAndPublish(language_3, contentletLanguage1Live);
final Contentlet contentlet3Checkout = ContentletDataGen.checkout(contentletLanguage3Live);
final Contentlet contentletLanguage3Working = ContentletDataGen.checkin(contentlet3Checkout);

final Contentlet contentletLanguage1Live2 = new ContentletDataGen(contentType)
.languageId(language_1.getId())
.host(host)
.nextPersistedAndPublish();

final Contentlet contentletLanguage1Live3 = new ContentletDataGen(contentType)
.languageId(language_1.getId())
.host(host)
.nextPersistedAndPublish();

final Contentlet contentletLanguage1Live4 = new ContentletDataGen(contentType)
.languageId(language_1.getId())
.host(host)
.nextPersistedAndPublish();

final List<Contentlet> contentlets = List.of(contentletLanguage1Live,
contentletLanguage1Working, contentletLanguage2Live, contentletLanguage2Working,
contentletLanguage3Live, contentletLanguage3Working, contentletLanguage1Live2,
contentletLanguage1Live3,
contentletLanguage1Live4);

final Set<String> identifiers = contentlets.stream().map(Contentlet::getIdentifier)
.collect(Collectors.toSet());

final ContentletAPI contentletAPI1 = APILocator.getContentletAPI();
final List<Contentlet> allVersions = contentletAPI1.findAllVersions(identifiers, APILocator.systemUser(), false);

Assert.assertEquals(9, allVersions.size());

for (Contentlet c:contentlets) {
Assert.assertTrue(allVersions.stream().anyMatch(contentlet -> contentlet.getIdentifier().equals(c.getIdentifier())));
Assert.assertTrue(allVersions.stream().anyMatch(contentlet -> contentlet.getInode().equals(c.getInode())));
}
//Random dude with no permissions
final User randomUser = new UserDataGen().nextPersisted();
Assert.assertTrue(contentletAPI1.findAllVersions(identifiers, randomUser, false).isEmpty());
}


/**
* Method to test: {@link ESContentletAPIImpl#copyContentToVariant(Contentlet, String, User)}
* When:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.dotcms.rest.api.v1.asset;

import static com.dotcms.util.CollectionsUtils.map;

import com.amazonaws.services.rekognition.model.Asset;
import com.dotcms.contenttype.exception.NotFoundInDbException;
import com.dotcms.datagen.ContentletDataGen;
import com.dotcms.datagen.FileAssetDataGen;
Expand All @@ -18,6 +21,7 @@
import com.dotcms.util.IntegrationTestInitService;
import com.dotcms.variant.model.Variant;
import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
Expand All @@ -31,12 +35,15 @@
import com.dotmarketing.util.UUIDGenerator;
import com.liferay.portal.model.User;
import com.liferay.portal.util.WebKeys;
import io.vavr.control.Try;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.RandomStringUtils;
import org.glassfish.jersey.internal.util.Base64;
Expand Down Expand Up @@ -436,7 +443,6 @@ public void Test_Delete_File_Then_Delete_Folder() throws DotDataException, DotSe
* @throws DotSecurityException
* @throws IOException
*/

@Test
public void Test_Retrieve_All_Versions()
throws DotDataException, DotSecurityException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
import static com.dotcms.content.elasticsearch.business.ESContentletAPIImpl.MAX_LIMIT;
import static com.dotcms.content.elasticsearch.business.ESIndexAPI.INDEX_OPERATIONS_TIMEOUT_IN_MS;
import static com.dotcms.variant.VariantAPI.DEFAULT_VARIANT;
import static com.dotmarketing.db.DbConnectionFactory.isPostgres;
import static com.dotmarketing.portlets.contentlet.model.Contentlet.AUTO_ASSIGN_WORKFLOW;
import static com.dotmarketing.portlets.contentlet.model.Contentlet.TITLE_IMAGE_KEY;
import static com.dotmarketing.portlets.contentlet.model.Contentlet.WORKFLOW_ACTION_KEY;
Expand Down Expand Up @@ -1059,6 +1058,33 @@ public List<Contentlet> findAllVersions(final Identifier identifier,
return findContentlets(inodes);
}


/**
* Find all versions for the given set of identifiers
* @param identifiers
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
@Override
public List<Contentlet> findAllVersions(final Set<String> identifiers)
throws DotDataException, DotSecurityException {
final DotConnect dc = new DotConnect();
final String query = "SELECT inode FROM contentlet c INNER JOIN contentlet_version_info cvi \n"
+ "ON (c.inode = cvi.working_inode OR c.inode = cvi.live_inode) \n"
+ "WHERE c.identifier IN (?) order by c.mod_date desc";
final String parameterPlaceholders = DotConnect.createParametersPlaceholder(identifiers.size());
dc.setSQL(query.replace("?", parameterPlaceholders));
for ( final String identifier : identifiers) {
dc.addParam(identifier);
}
@SuppressWarnings("unchecked")
final List<Map<String,Object>> list = dc.loadResults();
final List<String> inodes = list.stream().map(map -> map.get("inode").toString())
.collect(Collectors.toList());
return findContentlets(inodes);
}

@Override
protected List<Contentlet> findByStructure(String structureInode, int limit, int offset)
throws DotDataException, DotStateException, DotSecurityException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6944,6 +6944,33 @@ public List<Contentlet> findAllVersions(Identifier identifier, User user,
return findAllVersions(identifier, true, user, respectFrontendRoles);
}

/**
* Returns all versions of a contentlet given a set of identifiers
* @param identifiers
* @param user
* @param respectFrontendRoles
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
@Override
public List<Contentlet> findAllVersions(Set<String> identifiers, User user,
boolean respectFrontendRoles) throws DotDataException, DotSecurityException {

final List<Contentlet> allVersions = contentFactory.findAllVersions(identifiers);
if (allVersions.isEmpty()) {
return List.of();
}
return allVersions.stream().filter(contentlet -> {
try {
return permissionAPI.doesUserHavePermission(contentlet,
PermissionAPI.PERMISSION_READ, user, respectFrontendRoles);
} catch (DotDataException e) {
return false;
}
}).collect(Collectors.toList());
}

@CloseDBIfOpened
@Override
public List<Contentlet> findAllVersions(final Identifier identifier, final Variant variant,
Expand Down
Loading

0 comments on commit c5a888a

Please sign in to comment.