I have a question related to this saas topic.
When Admin creates a new Client I want to automatic fill Session Attributes with ascendant value numbers. How can I succeed that? Example A client’s number is 3 and I want it to give to the new client the next number: 4.
And I want to hide/disable the Remove button from the Session Attributes screen. How can I do that?
Create a BeforeInsert entity listener for the Group entity. See this example for how to add a listener to a platform entity.
> I want to hide/disable the Remove button from the Session Attributes screen
Create a Role and deny the Delete permission for the sec$SessionAttribute entity (it is system-level, so check the flag to find it on the Entities tab of the role editor). Keep in mind though, that users with a “Super” role (e.g. Administrators) will have all rights anyway.
> Create a BeforeInsert entity listener for the Group entity.
Okay, I created the entity listener. I know I need EntityManager for creating a query. But can I write an insert query with JPQL? Or I need JQPL only to know who is the last created client to update Session Attributes? Then how to insert into Session Attributes?
> that users with a “Super” role (e.g. Administrators) will have all rights anyway
Oh… No way to disable Remove button also to Super Administrators? I would need to globally disable, for no one delete the generated attributes, even not accidentally.
You have an instance of EntityManager in the listener method as an argument.
Create an instance of SessionAttribute entity and pass it to EntityManager.persist(). See here examples of creating entities using EntityManager (note that you shouldn’t create transactions in entity listener, because they are executed already in a transaction).
As for permissions for admins, you could remove Administrators role from them or make it non-super. If users have no explicitly denied permissions, they still have rights, so a user without roles is effectively an admin. See Roles - CUBA Platform. Developer’s Manual
@Component("myproject_GroupEntityListener")
public class GroupEntityListener implements BeforeInsertEntityListener<Group> {
@Inject
private Persistence persistence;
@Override
public void onBeforeInsert(Group entity, EntityManager entityManager) {
// get EntityManager for the current transaction
EntityManager em = persistence.getEntityManager();
// create and execute Query
Query query = em.createQuery(
"select e from sec_session_attr where e.deleted_by is null and e.delete_ts is null");
List<SessionAttribute> saList = query.getResultList();
String nextNumber = Integer.toString(saList.size()+1);
SessionAttribute sa = new SessionAttribute();
sa.setName("client_id");
sa.setDatatype("Integer");
sa.setStringValue(nextNumber);
// commit transaction
em.persist(sa);
}
}
And get this error:
“JpqlSyntaxException: Errors found for input jpql:[select e from sec_session_attr where e.deleted_by is null and e.delete_ts is null]
No entity variable name [Variable: null]”
Oookay, now I fixed the query, but now the client is created, but the session attribute did not inserted… What I’ve missed?
public void onBeforeInsert(Group entity, EntityManager entityManager) {
// create and execute Query
Query query = entityManager.createNativeQuery(
"SELECT e FROM sec_session_attr e WHERE e.deleted_by is null and e.delete_ts is null");
List<SessionAttribute> saList = query.getResultList();
String nextNumber = Integer.toString(saList.size()+1);
SessionAttribute sa = new SessionAttribute();
sa.setName("client_id");
sa.setDatatype("Integer");
sa.setStringValue(nextNumber);
entityManager.persist(sa); // commit sa??
}
I thought it works fine, except that if I create a new client, then in the Session Attributes the Data Type value will be “Datatype.Integer”. And I need only Integer, because this way it is not work. EDIT: we could solve this problem
I attached a photo.
The code is:
SessionAttribute sa = new SessionAttribute();
sa.setName("client_id");
/*EDIT:*/
//sa.setDatatype("Integer"); //old bad code
sa.setDatatype("int"); //working one
sa.setStringValue(nextNumber);
sa.setGroup(entity);