Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. package com.fishbowl.cache;
  2.  
  3. import com.fishbowl.cache.util.DynamicUsersLoader;
  4. import com.fishbowl.rest.RestApiClient;
  5. import org.apache.http.client.ClientProtocolException;
  6.  
  7. import java.util.HashMap;
  8. import java.util.Properties;
  9.  
  10. import com.google.gson.Gson;
  11.  
  12. import com.tangosol.net.CacheFactory;
  13. import com.tangosol.net.NamedCache;
  14. import com.tangosol.net.cache.CacheMap;
  15.  
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19.  
  20. import java.lang.reflect.Field;
  21.  
  22.  
  23. public class UsersCacheHandler {
  24. public static void refreshUsersCache(String actionPath) throws IOException,
  25. InstantiationException,
  26. IllegalAccessException,
  27. NoSuchFieldException {
  28. System.out.println("nas/cache/UsersCache.groovy");
  29. System.out.println("nas/cache/UsersCache.groovy: Caching PeopleSoft data");
  30.  
  31. // Initialize GSON
  32. Gson gson = new Gson();
  33.  
  34. // Load and clear cache
  35. NamedCache cache = CacheFactory.getCache("UsersCache");
  36. cache.clear();
  37.  
  38. // Get URL to call
  39. String apiURL = "https://adgstage.nasx.edu/ldapwsrest/user";
  40.  
  41. // Load API user and password
  42. Properties config = new Properties();
  43. File configFile =
  44. new File(actionPath + "/nas/search_users.properties");
  45. config.load(new FileInputStream(configFile));
  46. String apiUser = (String) config.get("SearchAPIUser");
  47. String apiPassword = (String) config.get("SearchAPIPassword");
  48.  
  49. // Make API call and parse results
  50. String apiResponse =
  51. RestApiClient.getProtectedJSON(apiURL, apiUser, apiPassword);
  52. HashMap[] users = gson.fromJson(apiResponse, HashMap[].class);
  53.  
  54. // Generate dynamic UserObject class
  55. /*
  56. * fieldList is a list of tuples of the format:
  57. * (objField, psField, closure)
  58. * where objField is the name of the field that will be added to the UserObject class,
  59. * psField is the name of the element in the PeopleSoft data that will be stored in
  60. * the UserObject field, and closure (which is optional) is a Groovy closure that will be
  61. * applied to the psField data before it is stored.
  62. */
  63. Object[][] fieldList =
  64. { { "firstName", "givenName" }, { "lastName", "sn" },
  65. { "unit", "description", new AnonymousBaseClass() {
  66. public String processValue(String description) {
  67. return description.indexOf("-") != -1 ?
  68. description.split("-")[0] : description;
  69. }
  70. } }, { "dept", "description", new AnonymousBaseClass() {
  71. public String processValue(String description) {
  72. return ~description.indexOf("-") != -1 ?
  73. description.split("-")[1] : description;
  74. }
  75. } }, { "board", "nasboard" },
  76. { "phone", "naspreferredofficephone", new AnonymousBaseClass() {
  77. public String processValue(String phone) {
  78. return phone.replaceAll("[^\\d]", "");
  79. }
  80. } }, { "title", "Nasemployeetitle" },
  81. { "unitDescription", "nasunitdescription" }, { "room", "nasroom" },
  82. { "email", "mail" } };
  83. Class<? extends UserObject> dynamicClass =
  84. DynamicUsersLoader.createDynamicClass(fieldList);
  85. // Add all users to the cache
  86. for (int i = 0; i < users.length; i++) {
  87. HashMap user = users[i];
  88. // Get new instance of dynamic UserObject
  89. UserObject userContainer = dynamicClass.newInstance();
  90. // For each field in the fieldList, get the value from the user data and add it to the object
  91. for (int j = 0; j < fieldList.length; j++) {
  92. Object[] field = fieldList[j];
  93. String objField = (String)field[0];
  94. String psField = (String)field[1];
  95. // Apply closure to data if necessary
  96. if (field.length == 3) {
  97. AnonymousBaseClass anonClass = (AnonymousBaseClass) field[2];
  98. user.put(psField, anonClass.processValue((String) user.get(psField)));
  99. }
  100. // Get and set field using reflection
  101. Field classField = dynamicClass.getDeclaredField(objField);
  102. classField.set(userContainer, user.get(psField));
  103. }
  104. // Put object into cache
  105. cache.put(((String) user.get("cn")).toLowerCase(), userContainer,
  106. CacheMap.EXPIRY_NEVER);
  107. }
  108.  
  109. System.out.println("nas/cache/UsersCache.groovy: Added PeopleSoft data to cache");
  110. }
  111.  
  112. private static abstract class AnonymousBaseClass {
  113. public abstract String processValue(String input);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement