Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 - Intermediary commit.
  • Loading branch information
dragospodariu96 committed Nov 24, 2020
1 parent 9e32dd9 commit 5ce32c7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@

package de.metas.marketing.base.api;

import com.google.common.collect.ImmutableSet;
import de.metas.user.UserId;
import de.metas.util.ISingletonService;

public interface IMKTGChannelDao extends ISingletonService
{
int retrieveMarketingChannelsCountForUser(UserId userId);

ImmutableSet<UserId> retrieveUsersHavingChannel(int marketingChannelId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package de.metas.marketing.base.api.impl;

import com.google.common.collect.ImmutableSet;
import de.metas.adempiere.model.I_AD_User;
import de.metas.marketing.base.api.IMKTGChannelDao;
import de.metas.marketing.base.model.I_AD_User_MKTG_Channels;
Expand All @@ -40,4 +41,17 @@ public int retrieveMarketingChannelsCountForUser(final UserId userId)
.create()
.count();
}

public ImmutableSet<UserId> retrieveUsersHavingChannel(final int marketingChannelId)
{
return Services.get(IQueryBL.class)
.createQueryBuilder(I_AD_User_MKTG_Channels.class)
.addEqualsFilter(I_AD_User_MKTG_Channels.COLUMNNAME_MKTG_Channel_ID, marketingChannelId)
.addOnlyActiveRecordsFilter()
.create()
.listDistinct(I_AD_User_MKTG_Channels.COLUMNNAME_AD_User_ID, Integer.class)
.stream()
.map(UserId::ofRepoId)
.collect(ImmutableSet.toImmutableSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import de.metas.util.Services;
import lombok.NonNull;
import org.adempiere.ad.callout.annotations.Callout;
import org.adempiere.ad.callout.annotations.CalloutMethod;
import org.adempiere.ad.modelvalidator.annotations.Interceptor;
import org.adempiere.ad.modelvalidator.annotations.ModelChange;
import org.adempiere.exceptions.AdempiereException;
Expand All @@ -46,14 +45,13 @@ private AD_User_MKTG_Channels()
{
}

@CalloutMethod(columnNames = I_AD_User_MKTG_Channels.COLUMNNAME_AD_User_ID)
@ModelChange(//
timings = { ModelValidator.TYPE_BEFORE_DELETE, ModelValidator.TYPE_BEFORE_CHANGE }, //
ifColumnsChanged = I_AD_User_MKTG_Channels.COLUMNNAME_AD_User_ID)
timings = { ModelValidator.TYPE_BEFORE_DELETE, ModelValidator.TYPE_BEFORE_CHANGE })
public void checkIfCanBeDeleted(@NonNull final I_AD_User_MKTG_Channels userMktgChannel)
{
int count = mktgChannelDao.retrieveMarketingChannelsCountForUser(UserId.ofRepoId(userMktgChannel.getAD_User_ID()));
if ( count == 1 ) {
if (count == 1)
{
throw new AdempiereException("User must have at least one marketing channel").markAsUserValidationError();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,49 @@

package de.metas.marketing.base.interceptor;

import de.metas.marketing.base.api.IMKTGChannelDao;
import de.metas.marketing.base.model.I_MKTG_Channel;
import de.metas.user.UserId;
import de.metas.util.Services;
import lombok.NonNull;
import org.adempiere.ad.callout.annotations.Callout;
import org.adempiere.ad.modelvalidator.annotations.Interceptor;
import org.adempiere.ad.modelvalidator.annotations.ModelChange;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.ModelValidator;

import java.util.Set;

@Callout(I_MKTG_Channel.class)
@Interceptor(I_MKTG_Channel.class)
public class MKTG_Channel
{
public static final MKTG_Channel INSTANCE = new MKTG_Channel();

private final IMKTGChannelDao mktgChannelDao = Services.get(IMKTGChannelDao.class);

private MKTG_Channel()
{
}

@ModelChange(//
timings = { ModelValidator.TYPE_BEFORE_DELETE, ModelValidator.TYPE_BEFORE_CHANGE })
public void checkIfCanBeDeleted(@NonNull final I_MKTG_Channel mktgChannel)
{
boolean canBeDeleted = true;
Set<UserId> usersSet = mktgChannelDao.retrieveUsersHavingChannel(mktgChannel.getMKTG_Channel_ID());
for (UserId userId : usersSet)
{
if (mktgChannelDao.retrieveMarketingChannelsCountForUser(userId) == 1)
{
canBeDeleted = false;
break;
}
}
if (canBeDeleted == false)
{
throw new AdempiereException("There are users only having this channel. Can't delete").markAsUserValidationError();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void registerInterceptors(final IModelValidationEngine engine)
protected void registerCallouts(final IProgramaticCalloutProvider calloutsRegistry)
{
calloutsRegistry.registerAnnotatedCallout(MKTG_Campaign_ContactPerson.INSTANCE);
calloutsRegistry.registerAnnotatedCallout(MKTG_Channel.INSTANCE);
calloutsRegistry.registerAnnotatedCallout(AD_User_MKTG_Channels.INSTANCE);
// calloutsRegistry.registerAnnotatedCallout(MKTG_Channel.INSTANCE);
// calloutsRegistry.registerAnnotatedCallout(AD_User_MKTG_Channels.INSTANCE);
}
}

0 comments on commit 5ce32c7

Please sign in to comment.