Is there a way to put some values in context when opening a window?

91 views
Skip to first unread message

Nicolas Micoud

unread,
Oct 17, 2024, 10:32:35 AM10/17/24
to iDempiere
Hi,

I have a very specific requirement.
Some tabs of a window should be set in ReadOnly mode according to the access rights of the users on the main record.

In order to give an example, you can imagine a XXX_BPartner_User table with following columns:
 - C_BPartner_ID
 - AD_User_ID
 - UpdateableTabs (ChosenMultipleTable)

For every BPartner, we can say which tab a user can update.

To resolve this, I think I can use AD_Tab.ReadOnlyLogic field with a SQL query but I fear that the query will be continuously executed (on each refresh, save, etc.). And as the result will always be the same, I don't this this is a nice solution.

So I wanted to put some values in context.

But I can't find a way to do this when the window opens.
The more close thing I can think of is the PredefinedVariables.
But as they are defined per window, that can't be used.

Is there a way to call a Java class when opening a window?
Or do you see an idea?

Thanks,

Nicolas

Jesús Castillo

unread,
Oct 17, 2024, 11:51:15 AM10/17/24
to idem...@googlegroups.com
Hi Nicolas,

Wow that seems very difficult to maintain, anyway I remind you that you can define context variables for a window, and also for roles. 
But if that doesn't work for you, you can set a value in some field with a callout. 
Callouts are executed every time a column or a record changes.
 
Atte: Jesus Castillo.


--
You received this message because you are subscribed to the Google Groups "iDempiere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to idempiere+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/idempiere/c649c015-c413-495e-bb94-e492b2fbb41fn%40googlegroups.com.

Carlos Antonio Ruiz Gomez

unread,
Oct 17, 2024, 12:44:40 PM10/17/24
to idem...@googlegroups.com
Hi Nicolas, not 100% sure, but I think the ReadOnlyLogic on Tab is
evaluated just when opening the window, and not when navigating every
record.

If that's the case, then your idea won't work, AFAIR for such
requirement you need to set ReadOnlyLogic on every field.

Regards,

Carlos Ruiz

Anozi Mada

unread,
Oct 17, 2024, 11:09:27 PM10/17/24
to iDempiere
Hi Nicolas,

Instead of relying on read only, how about using Before Save on those tabs that respect configuration in XXX_BPartner_User?
You can throw an error if they don't have access. You can also cache XXX_BPartner_User results if needed.

Regards,
Anozi Mada

Nicolas Micoud

unread,
Oct 18, 2024, 1:47:15 AM10/18/24
to iDempiere
Hi all,

Thanks for answering.

I've made some tests and AD_Tab.ReadOnlyLogic doesn't support @SQL notation and is called when navigating, not just when window opens.
Thus it cannot be used :(

I planned to add some beforeSave method to check if user can save records. That will work but is not a 'nice' solution (as user can try to make change and get an error message when saving).
Anyway is a way to be sure that record won't be changed (a kind of extra security).

At the end, I see no other option but to make change directly in GridTab.isReadOnly().
And add there some checks because current tab will be XXX_TableThatNeedsToBeChecked.
First I'll check if XXX_SetTabReadOnlyForThisTab is present in the context.
If so, use it.
Otherwise, execute some query to determine is it should be set as ReadOnly or not, and add the value in the context.
This way, the query will be executed once per window.

Not ideal, but I see no other options.

Regards,

Nicolas

Heng Sin Low

unread,
Oct 18, 2024, 3:27:29 AM10/18/24
to idem...@googlegroups.com
Perhaps you can set some context value using callout - https://wiki.idempiere.org/en/NF2.1_Callout_on_Navigate

--
You received this message because you are subscribed to the Google Groups "iDempiere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to idempiere+...@googlegroups.com.

Nicolas Micoud

unread,
Oct 18, 2024, 6:35:36 AM10/18/24
to iDempiere
Yes, is working fine with callout (or at least, I didn't notice any errors in my first tests), thanks for the hint!
The only thing I notice is the New button is enabled when the record has no row (but when clicking on it, tab become ReadOnly, and there should always be records when users will access the window)

Here's the code of the callout if needed:

private final static String AFFAIRE_REGISTRE_READ_ONLY_PREFIX = "XXA_AffaireRegistreReadOnly";


private void affaireRegistreItem(Properties ctx, int WindowNo, GridTab mTab, Object value) {


String readOnlyLogic = MTab.get(mTab.getAD_Tab_ID()).getReadOnlyLogic();


if (!Util.isEmpty(readOnlyLogic) && readOnlyLogic.startsWith("@" + AFFAIRE_REGISTRE_READ_ONLY_PREFIX)) {


int pos = readOnlyLogic.indexOf("@", AFFAIRE_REGISTRE_READ_ONLY_PREFIX.length());

String context = readOnlyLogic.substring(1, pos);


if (!Util.isEmpty(Env.getContext(Env.getCtx(), WindowNo, context)))

return;


String type = readOnlyLogic.substring(AFFAIRE_REGISTRE_READ_ONLY_PREFIX.length() + 1, pos);

int registreCatID = MXXARegistreCategory.get(ctx, Env.getAD_Client_ID(ctx), type, null).getXXA_RegistreCategory_ID();


boolean canUpdate = MXXAAffaireUser.canUpdate(ctx, Env.getContextAsInt(ctx, WindowNo, "XXA_Affaire_ID"), Env.getAD_User_ID(ctx), registreCatID, null);

Env.setContext(Env.getCtx(), WindowNo, context, canUpdate);

}

}



And ReadOnlyLogic is
2024-10-18 12_32_26-Expert Light DEV - Vivaldi.png

nb: there are several tabs based on the same table, that's why there is a type variable


Regards,

Nicolas

Martin Schönbeck

unread,
Oct 20, 2024, 12:53:43 PM10/20/24
to iDempiere
Hi Nicolas,

perhaps you could create an wiki entry somewhere in the snippets area: https://wiki.idempiere.org/en/Category:Code_snippets

Regards,
Martin

Nicolas Micoud

unread,
Oct 29, 2024, 9:00:17 AM10/29/24
to iDempiere
Hi Martin,
Done - thanks for the suggestion

Regards,

Nicolas
Reply all
Reply to author
Forward
0 new messages