Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fishbowl.cache;
- import com.fishbowl.cache.util.DynamicUsersLoader;
- import com.fishbowl.rest.RestApiClient;
- import org.apache.http.client.ClientProtocolException;
- import java.util.HashMap;
- import java.util.Properties;
- import com.google.gson.Gson;
- import com.tangosol.net.CacheFactory;
- import com.tangosol.net.NamedCache;
- import com.tangosol.net.cache.CacheMap;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.lang.reflect.Field;
- public class UsersCacheHandler {
- public static void refreshUsersCache(String actionPath) throws IOException,
- InstantiationException,
- IllegalAccessException,
- NoSuchFieldException {
- System.out.println("nas/cache/UsersCache.groovy");
- System.out.println("nas/cache/UsersCache.groovy: Caching PeopleSoft data");
- // Initialize GSON
- Gson gson = new Gson();
- // Load and clear cache
- NamedCache cache = CacheFactory.getCache("UsersCache");
- cache.clear();
- // Get URL to call
- String apiURL = "https://adgstage.nasx.edu/ldapwsrest/user";
- // Load API user and password
- Properties config = new Properties();
- File configFile =
- new File(actionPath + "/nas/search_users.properties");
- config.load(new FileInputStream(configFile));
- String apiUser = (String) config.get("SearchAPIUser");
- String apiPassword = (String) config.get("SearchAPIPassword");
- // Make API call and parse results
- String apiResponse =
- RestApiClient.getProtectedJSON(apiURL, apiUser, apiPassword);
- HashMap[] users = gson.fromJson(apiResponse, HashMap[].class);
- // Generate dynamic UserObject class
- /*
- * fieldList is a list of tuples of the format:
- * (objField, psField, closure)
- * where objField is the name of the field that will be added to the UserObject class,
- * psField is the name of the element in the PeopleSoft data that will be stored in
- * the UserObject field, and closure (which is optional) is a Groovy closure that will be
- * applied to the psField data before it is stored.
- */
- Object[][] fieldList =
- { { "firstName", "givenName" }, { "lastName", "sn" },
- { "unit", "description", new AnonymousBaseClass() {
- public String processValue(String description) {
- return description.indexOf("-") != -1 ?
- description.split("-")[0] : description;
- }
- } }, { "dept", "description", new AnonymousBaseClass() {
- public String processValue(String description) {
- return ~description.indexOf("-") != -1 ?
- description.split("-")[1] : description;
- }
- } }, { "board", "nasboard" },
- { "phone", "naspreferredofficephone", new AnonymousBaseClass() {
- public String processValue(String phone) {
- return phone.replaceAll("[^\\d]", "");
- }
- } }, { "title", "Nasemployeetitle" },
- { "unitDescription", "nasunitdescription" }, { "room", "nasroom" },
- { "email", "mail" } };
- Class<? extends UserObject> dynamicClass =
- DynamicUsersLoader.createDynamicClass(fieldList);
- // Add all users to the cache
- for (int i = 0; i < users.length; i++) {
- HashMap user = users[i];
- // Get new instance of dynamic UserObject
- UserObject userContainer = dynamicClass.newInstance();
- // For each field in the fieldList, get the value from the user data and add it to the object
- for (int j = 0; j < fieldList.length; j++) {
- Object[] field = fieldList[j];
- String objField = (String)field[0];
- String psField = (String)field[1];
- // Apply closure to data if necessary
- if (field.length == 3) {
- AnonymousBaseClass anonClass = (AnonymousBaseClass) field[2];
- user.put(psField, anonClass.processValue((String) user.get(psField)));
- }
- // Get and set field using reflection
- Field classField = dynamicClass.getDeclaredField(objField);
- classField.set(userContainer, user.get(psField));
- }
- // Put object into cache
- cache.put(((String) user.get("cn")).toLowerCase(), userContainer,
- CacheMap.EXPIRY_NEVER);
- }
- System.out.println("nas/cache/UsersCache.groovy: Added PeopleSoft data to cache");
- }
- private static abstract class AnonymousBaseClass {
- public abstract String processValue(String input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement