Skip to content

Commit

Permalink
#30950 now when an existing contentlet tries to fire a workflow in a … (
Browse files Browse the repository at this point in the history
#30992)

Previously this change when firing a workflow by action id to an
existing contentlet but in a diff language, wasnt working
Basically the isWorking perform over the new language which does not
exist, this new change test a third option which is look for a version
info in another language to proceed
  • Loading branch information
jdotcms authored Dec 26, 2024
1 parent f4b9561 commit 35f8c1b
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,18 @@ public interface VersionableAPI {
* @throws DotSecurityException
*/
boolean isWorking(Versionable versionable) throws DotDataException, DotStateException,DotSecurityException;


/**
* Tells if has working version in any language.
*
* @param versionable
* @return true if it has the working version. False if not
* @throws DotDataException
* @throws DotStateException
* @throws DotSecurityException
*/
boolean hasWorkingVersionInAnyOtherLanguage(Versionable versionable, long versionableLanguageId) throws DotDataException, DotStateException,DotSecurityException;

/**
* Sets the versionable as the working version for its identifier
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.dotcms.variant.model.Variant;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.beans.VersionInfo;
import com.dotmarketing.common.db.DotConnect;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
Expand Down Expand Up @@ -350,6 +351,24 @@ public boolean isLocked(final Versionable versionable) throws DotDataException,
}
}

@CloseDBIfOpened
@Override
public boolean hasWorkingVersionInAnyOtherLanguage(Versionable versionable, final long versionableLanguageId) throws DotDataException, DotStateException, DotSecurityException {

if(!UtilMethods.isSet(versionable) || !InodeUtils.isSet(versionable.getVersionId())) {
return false;
}

final Identifier identifier = APILocator.getIdentifierAPI().find(versionable);
if(identifier==null || !UtilMethods.isSet(identifier.getId()) || !UtilMethods.isSet(identifier.getAssetType())) {
return false;
}

// only contents are multi language
return "contentlet".equals(identifier.getAssetType())?
!this.versionableFactory.getWorkingVersionsExcludingLanguage(identifier.getId(), versionableLanguageId).isEmpty():false;
}

@CloseDBIfOpened
@Override
public boolean isWorking(final Versionable versionable) throws DotDataException, DotStateException, DotSecurityException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dotcms.variant.model.Variant;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.dotmarketing.beans.Identifier;
Expand Down Expand Up @@ -108,6 +109,15 @@ public abstract class VersionableFactory {
*/
protected abstract VersionInfo getVersionInfo(String identifier) throws DotDataException, DotStateException;

/**
* Get a list of all the working versions of a contentlet excluding the one with the specified language id.
* @param identifier
* @param lang
* @return List of the rows (working_inode + lang)
* @throws DotDataException
*/
protected abstract List<Map<String, Object>> getWorkingVersionsExcludingLanguage(String identifier, long lang) throws DotDataException;

/**
*
* @param identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -239,6 +240,13 @@ protected List<Versionable> findAllVersions(final String id, final Optional<Int
}
}

@Override
protected List<Map<String, Object>> getWorkingVersionsExcludingLanguage(final String identifier, final long lang) throws DotDataException {

return new DotConnect().setSQL("select working_inode, lang from contentlet_version_info where identifier = ? and lang != ?")
.addParam(identifier).addParam(lang).loadObjectResults();
}

@Override
protected VersionInfo getVersionInfo(String identifier) throws DotDataException,
DotStateException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3530,7 +3530,8 @@ public void validateActionStepAndWorkflow(final Contentlet contentlet, final Use
try {

final boolean isValidContentlet = !InodeUtils.isSet(contentlet.getInode())
|| contentlet.isWorking();
|| contentlet.isWorking() ||
APILocator.getVersionableAPI().hasWorkingVersionInAnyOtherLanguage(contentlet, contentlet.getLanguageId());
if (!isValidContentlet) {

throw new IllegalArgumentException(LanguageUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import java.util.List;
import java.util.Random;

import graphql.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -488,4 +490,100 @@ public void getEmptyVariant() throws DotDataException {

assertTrue(allByVariant.isEmpty());
}

/**
* Method to test: {@link VersionableAPI#isWorking(Versionable)}
* When: Create a {@link Contentlet} but do not save it, call the isWorking
* Should: should false
*/
@Test
public void test_is_working_with_non_persisted_contentlet() throws DotDataException, DotSecurityException {

final ContentType contentType = APILocator.getContentTypeAPI(APILocator.systemUser()).find("webPageContent");
final Contentlet myContentlet = new ContentletDataGen(contentType).next();

Assert.assertFalse(myContentlet.isWorking());
}

/**
* Method to test: {@link VersionableAPI#isWorking(Versionable)}
* When: Create a {@link Contentlet} and save it, call the isWorking
* Should: should true
*/
@Test
public void test_is_working_with_persisted_contentlet() throws DotDataException, DotSecurityException {

final ContentType contentType = APILocator.getContentTypeAPI(APILocator.systemUser()).find("webPageContent");
final Contentlet myContentlet = new ContentletDataGen(contentType)
.setProperty("title","Test").setProperty("body","Test Body")
.nextPersisted();

Assert.assertTrue(myContentlet.isWorking());
}

/**
* Method to test: {@link VersionableAPI#isWorking(Versionable)}
* When: Create a {@link Contentlet} and save it, call the isWorking
* Next, create another languages.
* Next, create a version but in that new language, call the isWorking
* Should: should false
*/
@Test
public void test_is_working_with_persisted_in_diff_lang_contentlet() throws DotDataException, DotSecurityException {

final ContentType contentType = APILocator.getContentTypeAPI(APILocator.systemUser()).find("webPageContent");
final Contentlet myContentlet = new ContentletDataGen(contentType)
.setProperty("title","Test").setProperty("body","Test Body")
.nextPersisted();
final String countryCode = "it";
final String languageCode = "it";
final Language languageIt = APILocator.getLanguageAPI().getLanguage(languageCode, countryCode)==null?
new LanguageDataGen().countryCode(countryCode).languageCode(languageCode).nextPersisted():
APILocator.getLanguageAPI().getLanguage(languageCode, countryCode);

final Contentlet myContentletIt = new ContentletDataGen(contentType)
.setProperty("title","Test").setProperty("body","Test Body")
.languageId(languageIt.getId())
.next();

Assert.assertTrue(myContentlet.isWorking());

myContentletIt.setIdentifier(myContentlet.getIdentifier());

Assert.assertFalse(myContentletIt.isWorking());
}

/**
* Method to test: {@link VersionableAPI#isWorking(Versionable)}
* When: Create a {@link Contentlet} and save it, call the isWorking
* Next, create another languages.
* Next, create a version but in that new language, call the isWorking
* Additionally calls the {@link VersionableAPI#hasWorkingVersionInAnyOtherLanguage(Versionable, long)} that may return true
*/
@Test
public void test_is_working_with_persisted_in_any_lang_contentlet() throws DotDataException, DotSecurityException {

final ContentType contentType = APILocator.getContentTypeAPI(APILocator.systemUser()).find("webPageContent");
final Contentlet myContentlet = new ContentletDataGen(contentType)
.setProperty("title","Test").setProperty("body","Test Body")
.nextPersisted();
final String countryCode = "it";
final String languageCode = "it";
final Language languageIt = APILocator.getLanguageAPI().getLanguage(languageCode, countryCode)==null?
new LanguageDataGen().countryCode(countryCode).languageCode(languageCode).nextPersisted():
APILocator.getLanguageAPI().getLanguage(languageCode, countryCode);

final Contentlet myContentletIt = new ContentletDataGen(contentType)
.setProperty("title","Test").setProperty("body","Test Body")
.languageId(languageIt.getId())
.next();

Assert.assertTrue(myContentlet.isWorking());

myContentletIt.setIdentifier(myContentlet.getIdentifier());

Assert.assertFalse(myContentletIt.isWorking());

Assert.assertTrue(APILocator.getVersionableAPI().hasWorkingVersionInAnyOtherLanguage(myContentletIt, languageIt.getId()));
}
}
Loading

0 comments on commit 35f8c1b

Please sign in to comment.