Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPD-48968 Lazy load for account groups #5927

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion modules/apps/account/account-api/bnd.bnd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Bundle-Name: Liferay Account API
Bundle-SymbolicName: com.liferay.account.api
Bundle-Version: 28.0.1
Bundle-Version: 28.1.0
Export-Package:\
com.liferay.account.configuration,\
com.liferay.account.constants,\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public AccountEntry addAccountEntry(
ServiceContext serviceContext)
throws PortalException;

public AccountEntry addIncompleteAccountEntry(
String externalReferenceCode, long companyId, long userId,
String name, String type)
throws Exception;

public AccountEntry addOrUpdateAccountEntry(
String externalReferenceCode, long userId,
long parentAccountEntryId, String name, String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public static AccountEntry addAccountEntry(
emailAddress, logoBytes, taxIdNumber, type, status, serviceContext);
}

public static AccountEntry addIncompleteAccountEntry(
String externalReferenceCode, long companyId, long userId,
String name, String type)
throws Exception {

return getService().addIncompleteAccountEntry(
externalReferenceCode, companyId, userId, name, type);
}

public static AccountEntry addOrUpdateAccountEntry(
String externalReferenceCode, long userId,
long parentAccountEntryId, String name, String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public com.liferay.account.model.AccountEntry addAccountEntry(
emailAddress, logoBytes, taxIdNumber, type, status, serviceContext);
}

@Override
public com.liferay.account.model.AccountEntry addIncompleteAccountEntry(
String externalReferenceCode, long companyId, long userId,
String name, String type)
throws Exception {

return _accountEntryLocalService.addIncompleteAccountEntry(
externalReferenceCode, companyId, userId, name, type);
}

@Override
public com.liferay.account.model.AccountEntry addOrUpdateAccountEntry(
String externalReferenceCode, long userId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version 14.1.0
version 14.2.0
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.liferay.account.validator.AccountEntryEmailAddressValidatorFactory;
import com.liferay.asset.kernel.service.AssetEntryLocalService;
import com.liferay.expando.kernel.service.ExpandoRowLocalService;
import com.liferay.lazy.referencing.kernel.LazyReferencingThreadLocal;
import com.liferay.petra.function.transform.TransformUtil;
import com.liferay.petra.sql.dsl.DSLFunctionFactoryUtil;
import com.liferay.petra.sql.dsl.DSLQueryFactoryUtil;
Expand Down Expand Up @@ -228,13 +229,19 @@ GroupConstants.DEFAULT_LIVE_GROUP_ID, getLocalizationMap(name),

// Workflow

if (_isWorkflowEnabled(accountEntry.getCompanyId())) {
if (!LazyReferencingThreadLocal.isIncompleteModel() &&
_isWorkflowEnabled(accountEntry.getCompanyId())) {

_checkStatus(accountEntry.getStatus(), status);

accountEntry = _startWorkflowInstance(
userId, accountEntry, workflowServiceContext);
}
else {
if (LazyReferencingThreadLocal.isIncompleteModel()) {
status = WorkflowConstants.STATUS_INCOMPLETE;
}

accountEntry = updateStatus(
userId, accountEntryId, status, workflowServiceContext,
Collections.emptyMap());
Expand All @@ -243,6 +250,41 @@ GroupConstants.DEFAULT_LIVE_GROUP_ID, getLocalizationMap(name),
return accountEntry;
}

@Override
public AccountEntry addIncompleteAccountEntry(
String externalReferenceCode, long companyId, long userId,
String name, String type)
throws Exception {

if (!LazyReferencingThreadLocal.isLazyReferencingEnabled()) {
throw new UnsupportedOperationException();
}

AccountEntry accountEntry =
accountEntryLocalService.fetchAccountEntryByExternalReferenceCode(
externalReferenceCode, companyId);

if (accountEntry != null) {
return accountEntry;
}

try {
LazyReferencingThreadLocal.setIncompleteModel(true);

accountEntry = accountEntryLocalService.addAccountEntry(
userId, AccountConstants.PARENT_ACCOUNT_ENTRY_ID_DEFAULT,
GetterUtil.get(name, externalReferenceCode), StringPool.BLANK,
null, StringPool.BLANK, null, StringPool.BLANK, type,
WorkflowConstants.STATUS_INCOMPLETE, null);

return accountEntryLocalService.updateExternalReferenceCode(
accountEntry.getAccountEntryId(), externalReferenceCode);
}
finally {
LazyReferencingThreadLocal.setIncompleteModel(false);
}
}

@Override
public AccountEntry addOrUpdateAccountEntry(
String externalReferenceCode, long userId,
Expand Down Expand Up @@ -679,6 +721,10 @@ public AccountEntry updateAccountEntry(
workflowUserId = serviceContext.getUserId();
}

if (status == WorkflowConstants.STATUS_INCOMPLETE) {
status = WorkflowConstants.STATUS_APPROVED;
}

if (_isWorkflowEnabled(accountEntry.getCompanyId())) {
_checkStatus(accountEntry.getStatus(), status);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.liferay.expando.kernel.model.ExpandoColumnConstants;
import com.liferay.expando.kernel.model.ExpandoTableConstants;
import com.liferay.expando.test.util.ExpandoTestUtil;
import com.liferay.lazy.referencing.kernel.LazyReferencingThreadLocal;
import com.liferay.object.constants.ObjectValidationRuleConstants;
import com.liferay.object.exception.ObjectValidationRuleEngineException;
import com.liferay.object.model.ObjectDefinition;
Expand Down Expand Up @@ -450,6 +451,63 @@ public void testAddAccountEntryWithWorkflowEnabled() throws Exception {
Assert.assertTrue(_hasWorkflowInstance(accountEntry));
}

@Test
public void testAddIncompleteAccountEntry() throws Exception {
LazyReferencingThreadLocal.setLazyReferencingEnabled(true);

try {
AccountEntry accountEntry =
_accountEntryLocalService.addIncompleteAccountEntry(
RandomTestUtil.randomString(),
TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
RandomTestUtil.randomString(),
AccountConstants.ACCOUNT_ENTRY_TYPE_BUSINESS);

_assertStatus(
accountEntry, WorkflowConstants.STATUS_INCOMPLETE,
TestPropsValues.getUser());
}
finally {
LazyReferencingThreadLocal.setLazyReferencingEnabled(false);
}
}

@Test(expected = UnsupportedOperationException.class)
public void testAddIncompleteAccountEntryWithoutLazyReferencing()
throws Exception {

_accountEntryLocalService.addIncompleteAccountEntry(
RandomTestUtil.randomString(), TestPropsValues.getCompanyId(),
TestPropsValues.getUserId(), RandomTestUtil.randomString(),
AccountConstants.ACCOUNT_ENTRY_TYPE_BUSINESS);
}

@Test
public void testAddIncompleteAccountEntryWithWorkflowEnabled()
throws Exception {

LazyReferencingThreadLocal.setLazyReferencingEnabled(true);

try {
_enableWorkflow();

AccountEntry accountEntry =
_accountEntryLocalService.addIncompleteAccountEntry(
RandomTestUtil.randomString(),
TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
RandomTestUtil.randomString(),
AccountConstants.ACCOUNT_ENTRY_TYPE_BUSINESS);

_assertStatus(
accountEntry, WorkflowConstants.STATUS_INCOMPLETE,
TestPropsValues.getUser());
Assert.assertFalse(_hasWorkflowInstance(accountEntry));
}
finally {
LazyReferencingThreadLocal.setLazyReferencingEnabled(false);
}
}

@Test
public void testDeactivateAccountEntries() throws Exception {
List<AccountEntry> accountEntries =
Expand Down Expand Up @@ -1144,6 +1202,75 @@ public void testUpdateAccountEntry() throws Exception {
expandoBridge.getAttribute("customFieldName")));
}

@Test
public void testUpdateAccountEntryWithStatusIncomplete() throws Exception {
LazyReferencingThreadLocal.setLazyReferencingEnabled(true);

try {
AccountEntry accountEntry =
_accountEntryLocalService.addIncompleteAccountEntry(
RandomTestUtil.randomString(),
TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
RandomTestUtil.randomString(),
AccountConstants.ACCOUNT_ENTRY_TYPE_BUSINESS);

_assertStatus(
accountEntry, WorkflowConstants.STATUS_INCOMPLETE,
TestPropsValues.getUser());

accountEntry = _accountEntryLocalService.updateAccountEntry(
accountEntry.getAccountEntryId(),
accountEntry.getParentAccountEntryId(), accountEntry.getName(),
accountEntry.getDescription(), false, null,
accountEntry.getEmailAddress(), null,
accountEntry.getTaxIdNumber(), accountEntry.getStatus(), null);

_assertStatus(
accountEntry, WorkflowConstants.STATUS_APPROVED,
TestPropsValues.getUser());
}
finally {
LazyReferencingThreadLocal.setLazyReferencingEnabled(false);
}
}

@Test
public void testUpdateAccountEntryWithStatusIncompleteWorkflowEnabled()
throws Exception {

LazyReferencingThreadLocal.setLazyReferencingEnabled(true);

try {
_enableWorkflow();

AccountEntry accountEntry =
_accountEntryLocalService.addIncompleteAccountEntry(
RandomTestUtil.randomString(),
TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
RandomTestUtil.randomString(),
AccountConstants.ACCOUNT_ENTRY_TYPE_BUSINESS);

_assertStatus(
accountEntry, WorkflowConstants.STATUS_INCOMPLETE,
TestPropsValues.getUser());

accountEntry = _accountEntryLocalService.updateAccountEntry(
accountEntry.getAccountEntryId(),
accountEntry.getParentAccountEntryId(), accountEntry.getName(),
accountEntry.getDescription(), false, null,
accountEntry.getEmailAddress(), null,
accountEntry.getTaxIdNumber(), accountEntry.getStatus(), null);

_assertStatus(
accountEntry, WorkflowConstants.STATUS_PENDING,
TestPropsValues.getUser());
Assert.assertTrue(_hasWorkflowInstance(accountEntry));
}
finally {
LazyReferencingThreadLocal.setLazyReferencingEnabled(false);
}
}

@Test
public void testUpdateDomains() throws Exception {
AccountEntry accountEntry = AccountEntryTestUtil.addAccountEntry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void setExternalReferenceCode(
}

@GraphQLField(description = "The account's external reference code.")
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
protected String externalReferenceCode;

@JsonIgnore
Expand Down Expand Up @@ -166,7 +166,7 @@ public void setName(UnsafeSupplier<String, Exception> nameUnsafeSupplier) {
}

@GraphQLField(description = "The account's name.")
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
protected String name;

@JsonIgnore
Expand Down Expand Up @@ -214,6 +214,45 @@ public void setRoleBriefs(
@JsonIgnore
private Supplier<RoleBrief[]> _roleBriefsSupplier;

@Schema(description = "The account's type.")
public String getType() {
if (_typeSupplier != null) {
type = _typeSupplier.get();

_typeSupplier = null;
}

return type;
}

public void setType(String type) {
this.type = type;

_typeSupplier = null;
}

@JsonIgnore
public void setType(UnsafeSupplier<String, Exception> typeUnsafeSupplier) {
_typeSupplier = () -> {
try {
return typeUnsafeSupplier.get();
}
catch (RuntimeException runtimeException) {
throw runtimeException;
}
catch (Exception exception) {
throw new RuntimeException(exception);
}
};
}

@GraphQLField(description = "The account's type.")
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
protected String type;

@JsonIgnore
private Supplier<String> _typeSupplier;

@Override
public boolean equals(Object object) {
if (this == object) {
Expand Down Expand Up @@ -307,6 +346,22 @@ public String toString() {
sb.append("]");
}

String type = getType();

if (type != null) {
if (sb.length() > 1) {
sb.append(", ");
}

sb.append("\"type\": ");

sb.append("\"");

sb.append(_escape(type));

sb.append("\"");
}

sb.append("}");

return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void setAccountBriefs(
@GraphQLField(
description = "The list of accounts associated with this account group."
)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
protected AccountBrief[] accountBriefs;

@JsonIgnore
Expand Down Expand Up @@ -184,7 +184,7 @@ public void setCreator(
}

@GraphQLField(description = "The user who created the account group.")
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
protected Creator creator;

@JsonIgnore
Expand Down
Loading