Skip to Content
Author's profile photo Former Member

Use MDM4J to automatically update a mask

Masks are static

Masks in MDM are commonly misunderstood as SQL Views with a different name. A view always has some kind of query attached to it, which means when data changes the view is updated. A mask, on the other hand, is a mere static list of records. The misconception is also fueled by the fact most people will do a search, and add all the records from the result into a mask — to the user it feels like the mask is a connected to the query. I don’t want to encourage anyone to look at masks as views — there’s a reason masks are lists (performance reasons); I do however want to show how the Java API can be used to make a mask look like an SQL view (just for the exercise, to show off how nice the API is.) Note that there are a lot of consideratios to be taken into account if you plan to do something like this in a real projects — scalability, performance etc.

Using the API to make masks “activate”

The API gives us the ability to “listen” to add-record events (as well as record-changed and deleted events; look at the documentation for more information). Using these listeners we can be notified when records are added (or modified), inspect them to make sure the mask’s “query”, and add them or remove them from the mask.

Sample code

Again, this code is unrefined, and is here just for demonstration purposes. To compile and run make sure you include mdm4j.jar in your classpath, and that you have a running MDM server (the code is documented)

package sdn.ActiveMask; import a2i.common.CatalogData; import a2i.generated.RC; import a2i.core.StringException; import a2i.generated.CMTableInfo; import a2i.listener.RecordAddedListener; import a2i.listener.RecordAddedEvent; import a2i.common.ResultSetDefinition; import a2i.core.A2iStringArray; import a2i.core.A2iIntArray; import a2i.common.A2iResultSet; import a2i.core.Value; public class ActiveMask implements RecordAddedListener { private CatalogData catalog; public static void main (String[] args) { if (args.length<4) { System.out.println("Usage: java ActiveMask [host] [port] [user-name] [password]"); } else { // Create connection the server CatalogData catalog = new CatalogData(); String server = args[0]; int port = Integer.parseInt(args[1]); String user = args[2]; String password = args[3]; int result = catalog.Login(server, port, user, password, "English [US]"); if (result!=RC.RC_OK) { System.out.println(RC.GetTextForError(result)); } else { // Instantiate the object ActiveMask activeMask = new ActiveMask(catalog); System.out.println("Connection established."); // Register the add-record listener try { activeMask.catalog.AddRecordAddedListener(activeMask); // Let the thread run while(true) { Thread.yield(); try { Thread.sleep(500); } catch (InterruptedException ex) { System.out.println(ex); } } } catch (StringException ex) { System.out.println(ex); } } } } public ActiveMask(CatalogData catalog) { this.catalog = catalog; } public void recordAdded(RecordAddedEvent ev) { // Output new record ID and table name System.out.println("Record: " + String.valueOf(ev.GetRecordID()) + " added to table: " + ev.GetTableName()); A2iStringArray fields = new A2iStringArray(); A2iIntArray ids = new A2iIntArray(); ids.Add(ev.GetRecordID()); fields.Add("Name"); // read the new table from the repository -- get its "Name" field ResultSetDefinition rsd = new ResultSetDefinition(ev.GetTableName(),fields); try { A2iResultSet rs = catalog.GetRecordsById(rsd, ids); if (rs.GetRecordCount()>0) { Value value = rs.GetValueAt(0, "Name"); /* Check if the record meets our "query" -- in this case, does its name equal the string "TOMASK"? If yes, add it to the mask */ if (value.GetStringValue().equals("TOMASK")) { catalog.ModifyMaskByRecordIds("Masks", 1, ids, true); } } } catch (StringException se) { System.out.println(se); } } }


Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.