Search in sources :

Example 11 with JsonJavaObject

use of com.ibm.commons.util.io.json.JsonJavaObject in project org.openntf.domino by OpenNTF.

the class EngageDataInitializer method loadData.

@SuppressWarnings({ "unchecked", "unused" })
public void loadData(final org.openntf.domino.Session s, final FramedTransactionalGraph<DGraph> framedGraph) {
    HashMap<String, Location> locs = new HashMap<String, Location>();
    HashMap<String, Track> tracks = new HashMap<String, Track>();
    HashMap<String, String> trackLkup = new HashMap<String, String>();
    trackLkup.put("Special", "Sp");
    trackLkup.put("Strategy/Deployment", "Str");
    trackLkup.put("Administration", "Adm");
    trackLkup.put("Development", "Dev");
    trackLkup.put("Business", "Bus");
    trackLkup.put("Commercial", "Comm");
    try {
        String urlData = readUrl("http://xceed.be/engage.nsf/api/data/collections/name/BLUG_ViewSessions?start=0&count=100");
        JsonJavaFactory factory = JsonJavaFactory.instanceEx;
        ArrayList<JsonJavaObject> jsonData = (ArrayList<JsonJavaObject>) JsonParser.fromJson(factory, urlData);
        SimpleDateFormat sdf = new SimpleDateFormat();
        for (JsonJavaObject obj : jsonData) {
            String locKey = obj.getAsString("session_room");
            Location loc = framedGraph.addVertex(locKey, Location.class);
            if (Strings.isBlankString(loc.getName())) {
                loc.setName(locKey);
            }
            String trackKey = obj.getAsString("session_track");
            Track track = framedGraph.addVertex(trackLkup.get(trackKey), Track.class);
            if (Strings.isBlankString(track.getTitle())) {
                track.setTitle(trackLkup.get(trackKey));
                track.setDescription(trackKey);
            }
            // DO DATES
            String actualDate = obj.getAsString("session_date");
            String manipulated = actualDate.substring(0, 10) + " " + actualDate.substring(11, 19);
            Date startDate = DATE_FORMAT.parse(manipulated);
            String startTime = obj.getAsString("session_time1");
            String endTime = obj.getAsString("session_time2");
            Calendar startCal = Calendar.getInstance();
            startCal.setTime(startDate);
            startCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startTime.substring(0, 2)));
            startCal.set(Calendar.MINUTE, Integer.parseInt(startTime.substring(3, 5)));
            startCal.set(Calendar.SECOND, 0);
            startCal.set(Calendar.MILLISECOND, 0);
            Calendar endCal = Calendar.getInstance();
            endCal.setTime(startDate);
            endCal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endTime.substring(0, 2)));
            endCal.set(Calendar.MINUTE, Integer.parseInt(endTime.substring(3, 5)));
            endCal.set(Calendar.SECOND, 0);
            endCal.set(Calendar.MILLISECOND, 0);
            String tsKey = sdf.format(startCal.getTime()) + " - " + sdf.format(endCal.getTime());
            TimeSlot ts = framedGraph.addVertex(tsKey, TimeSlot.class);
            ts.setStartTime(startCal);
            ts.setEndTime(endCal);
            String code = obj.getAsString("session_nr");
            Presentation sess = framedGraph.addVertex(code, Presentation.class);
            sess.setTitle(obj.getAsString("session_title"));
            sess.setDescription(obj.getAsString("session_abstract"));
            sess.setStatus(Event.Status.CONFIRMED);
            sess.setSessionId(code);
            System.out.println("Assigning location - " + locKey + " to session " + obj.getAsString("session_title"));
            sess.addLocation(loc);
            track.addIncludesSession(sess);
            ts.addEvent(sess);
            for (int i = 1; i < 6; i++) {
                String suffix = "";
                if (i > 1) {
                    suffix = Integer.toString(i);
                }
                String speaker = obj.getAsString("speaker_name" + suffix);
                if ("".equals(speaker)) {
                    break;
                }
                String speakerName = speaker;
                String organization = obj.getAsString("speaker_org" + suffix);
                Attendee att = framedGraph.addVertex(null, Attendee.class);
                System.out.println(speaker);
                int sep = speakerName.indexOf(" ");
                if (sep > -1) {
                    String firstName = speakerName.substring(0, sep);
                    String lastName = speakerName.substring(sep + 1, speakerName.length());
                    att.setFirstName(firstName);
                    att.setLastName(lastName);
                } else {
                    att.setFirstName(speakerName);
                }
                att.setTwitterId(obj.getAsString("speaker_twitter" + suffix));
                att.setUrl(obj.getAsString("speaker_photourl" + suffix));
                if (!"".equals(organization)) {
                    Group org = framedGraph.addVertex(organization, Group.class);
                    org.setName(organization);
                    org.setType(Group.Type.COMPANY);
                    org.addMember(att);
                }
                sess.addPresentedBy(att);
                sess.addAttendingAttendee(att);
                sess.addPlansToAttend(att);
            }
        }
        framedGraph.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : JsonJavaFactory(com.ibm.commons.util.io.json.JsonJavaFactory) HashMap(java.util.HashMap) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Date(java.util.Date) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with JsonJavaObject

use of com.ibm.commons.util.io.json.JsonJavaObject in project org.openntf.nsfodp by OpenNTF.

the class StockComponentsServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setBufferSize(0);
    // $NON-NLS-1$ //$NON-NLS-2$
    resp.setHeader("Content-Type", "text/json");
    ServletOutputStream os = resp.getOutputStream();
    try {
        if (componentInfo == null) {
            SharableRegistryImpl facesRegistry = new SharableRegistryImpl(getClass().getPackage().getName());
            // $NON-NLS-1$
            List<Object> libraries = ExtensionManager.findServices((List<Object>) null, LibraryServiceLoader.class, "com.ibm.xsp.Library");
            libraries.stream().filter(lib -> lib instanceof XspLibrary).map(XspLibrary.class::cast).map(lib -> new LibraryWrapper(lib.getLibraryId(), lib)).map(wrapper -> {
                SimpleRegistryProvider provider = new SimpleRegistryProvider();
                provider.init(wrapper);
                return provider;
            }).map(XspRegistryProvider::getRegistry).forEach(facesRegistry::addDepend);
            facesRegistry.refreshReferences();
            componentInfo = new JsonJavaObject();
            JsonArray defs = new JsonJavaArray();
            facesRegistry.findComponentDefs().stream().filter(FacesComponentDefinition::isTag).map(def -> {
                try {
                    JsonObject defObj = new JsonJavaObject();
                    // $NON-NLS-1$
                    defObj.putJsonProperty("namespaceUri", def.getNamespaceUri());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("tagName", def.getTagName());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("class", def.getJavaClass().getName());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("since", def.getSince());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("defaultPrefix", def.getFirstDefaultPrefix());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("componentFamily", def.getComponentFamily());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("componentType", def.getComponentType());
                    // $NON-NLS-1$
                    defObj.putJsonProperty("id", def.getId());
                    JsonArray facetNames = new JsonJavaArray();
                    for (String facetName : def.getFacetNames()) {
                        facetNames.add(facetName);
                    }
                    // $NON-NLS-1$
                    defObj.putJsonProperty("facetNames", facetNames);
                    FacesProperty defaultProp = def.getDefaultFacesProperty();
                    if (defaultProp != null) {
                        JsonObject defaultPropObj = new JsonJavaObject();
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("name", defaultProp.getName());
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("since", defaultProp.getSince());
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("class", defaultProp.getJavaClass().getName());
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("required", defaultProp.isRequired());
                        // $NON-NLS-1$
                        defaultPropObj.putJsonProperty("attribute", defaultProp.isAttribute());
                    }
                    JsonArray properties = new JsonJavaArray();
                    for (String propName : def.getPropertyNames()) {
                        FacesProperty prop = def.getProperty(propName);
                        JsonObject propObj = new JsonJavaObject();
                        // $NON-NLS-1$
                        propObj.putJsonProperty("name", propName);
                        // $NON-NLS-1$
                        propObj.putJsonProperty("class", prop.getJavaClass().getName());
                        // $NON-NLS-1$
                        propObj.putJsonProperty("since", prop.getSince());
                        // $NON-NLS-1$
                        propObj.putJsonProperty("required", prop.isRequired());
                        // $NON-NLS-1$
                        propObj.putJsonProperty("attribute", prop.isAttribute());
                        properties.add(propObj);
                    }
                    // $NON-NLS-1$
                    defObj.putJsonProperty("properties", properties);
                    return defObj;
                } catch (JsonException e) {
                    throw new RuntimeException(e);
                }
            }).forEach(defObj -> {
                try {
                    defs.add(defObj);
                } catch (JsonException e) {
                    throw new RuntimeException(e);
                }
            });
            // $NON-NLS-1$
            componentInfo.putJsonProperty("components", defs);
        }
        os.print(JsonGenerator.toJson(JsonJavaFactory.instance, componentInfo, false));
    } catch (Throwable e) {
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(baos);
        e.printStackTrace(out);
        out.flush();
        os.println(baos.toString());
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) ExtensionManager(com.ibm.commons.extension.ExtensionManager) SimpleRegistryProvider(com.ibm.xsp.registry.config.SimpleRegistryProvider) JsonGenerator(com.ibm.commons.util.io.json.JsonGenerator) XspLibrary(com.ibm.xsp.library.XspLibrary) HttpServletRequest(javax.servlet.http.HttpServletRequest) JsonJavaArray(com.ibm.commons.util.io.json.JsonJavaArray) FacesComponentDefinition(com.ibm.xsp.registry.FacesComponentDefinition) JsonJavaFactory(com.ibm.commons.util.io.json.JsonJavaFactory) ServletOutputStream(javax.servlet.ServletOutputStream) JsonArray(com.ibm.commons.util.io.json.JsonArray) LibraryWrapper(com.ibm.xsp.library.LibraryWrapper) PrintWriter(java.io.PrintWriter) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) SharableRegistryImpl(com.ibm.xsp.registry.SharableRegistryImpl) HttpServlet(javax.servlet.http.HttpServlet) JsonObject(com.ibm.commons.util.io.json.JsonObject) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) FacesProperty(com.ibm.xsp.registry.FacesProperty) WebServlet(javax.servlet.annotation.WebServlet) LibraryServiceLoader(com.ibm.xsp.library.LibraryServiceLoader) List(java.util.List) JsonException(com.ibm.commons.util.io.json.JsonException) XspRegistryProvider(com.ibm.xsp.registry.config.XspRegistryProvider) JsonException(com.ibm.commons.util.io.json.JsonException) JsonJavaArray(com.ibm.commons.util.io.json.JsonJavaArray) LibraryWrapper(com.ibm.xsp.library.LibraryWrapper) ServletOutputStream(javax.servlet.ServletOutputStream) XspLibrary(com.ibm.xsp.library.XspLibrary) JsonObject(com.ibm.commons.util.io.json.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SharableRegistryImpl(com.ibm.xsp.registry.SharableRegistryImpl) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) JsonArray(com.ibm.commons.util.io.json.JsonArray) FacesComponentDefinition(com.ibm.xsp.registry.FacesComponentDefinition) SimpleRegistryProvider(com.ibm.xsp.registry.config.SimpleRegistryProvider) JsonJavaObject(com.ibm.commons.util.io.json.JsonJavaObject) JsonObject(com.ibm.commons.util.io.json.JsonObject) FacesProperty(com.ibm.xsp.registry.FacesProperty) PrintWriter(java.io.PrintWriter)

Aggregations

JsonJavaObject (com.ibm.commons.util.io.json.JsonJavaObject)12 JsonException (com.ibm.commons.util.io.json.JsonException)8 IOException (java.io.IOException)8 List (java.util.List)8 CaseInsensitiveString (org.openntf.domino.types.CaseInsensitiveString)8 ArrayList (java.util.ArrayList)7 WebApplicationException (javax.ws.rs.WebApplicationException)6 UserAccessException (org.openntf.domino.exceptions.UserAccessException)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 VertexFrame (com.tinkerpop.frames.VertexFrame)4 StringReader (java.io.StringReader)4 StringWriter (java.io.StringWriter)4 Response (javax.ws.rs.core.Response)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)4 JsonGraphFactory (org.openntf.domino.rest.json.JsonGraphFactory)4 JsonGraphWriter (org.openntf.domino.rest.json.JsonGraphWriter)4 EdgeFrame (com.tinkerpop.frames.EdgeFrame)3 PrintWriter (java.io.PrintWriter)3