private Point createFeedPoint(final User user) throws NimbitsException { final EntityName name = CommonFactoryLocator.getInstance().createName(Const.TEXT_DATA_FEED, EntityType.point); final Entity entity = EntityModelFactory.createEntity( name, "", EntityType.feed, ProtectionLevel.onlyConnection, user.getKey(), user.getKey(), UUID.randomUUID().toString()); // final Entity r = EntityServiceFactory.getInstance().addUpdateEntity(user, entity); Point point = PointModelFactory.createPointModel(entity); final Point result = (Point) EntityServiceFactory.getInstance().addUpdateEntity(point); postToFeed( user, "A new data point has been created for your data feed. Your data feed is just " + "a data point. Points are capable of storing numbers, text, json and xml data. Nimbits uses " + "a single data point to drive this feed.", FeedType.info); return result; }
public final class LauncherProfiles { public static LauncherProfiles INSTANCE; static void modified() { try (FileWriter writer = new FileWriter(getFile())) { JsonObject obj = deepCopy(INSTANCE.everythingElse); obj.addProperty("clientToken", INSTANCE.clientToken); obj.add("authenticationDatabase", GSON.toJsonTree(INSTANCE.authenticationDatabase.profiles)); GSON.toJson(obj, writer); System.out.println("Profiles saved"); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public static <T extends JsonElement> T deepCopy(T from) { if (from == null) { throw new IllegalArgumentException("from must not be null"); } if (from instanceof JsonObject) { return (T) deepCopyObj((JsonObject) from); } if (from instanceof JsonArray) { return (T) deepCopyArr((JsonArray) from); } if (from instanceof JsonNull) { return (T) JsonNull.INSTANCE; } if (from instanceof JsonPrimitive) { // Nulls and primitives are immutable return from; } throw new AssertionError("Unknown element type " + from.getClass().getName()); } private static JsonObject deepCopyObj(JsonObject from) { JsonObject result = new JsonObject(); for (Map.Entry<String, JsonElement> entry : from.entrySet()) { result.add(entry.getKey(), deepCopy(entry.getValue())); } return result; } private static JsonArray deepCopyArr(JsonArray from) { JsonArray result = new JsonArray(); for (JsonElement element : from) { result.add(deepCopy(element)); } return result; } public static LauncherProfiles load() throws IOException { System.out.println("Loading Minecraft profiles from " + DevLauncher.workingDirectory); try (FileReader reader = new FileReader(getFile())) { LauncherProfiles profiles = new LauncherProfiles(); JsonObject e = new JsonParser().parse(reader).getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : e.entrySet()) { if (entry.getKey().equals("clientToken")) { profiles.clientToken = entry.getValue().getAsString(); } else if (entry.getKey().equals("authenticationDatabase")) { JsonObject o = entry.getValue().getAsJsonObject(); for (Map.Entry<String, JsonElement> entry1 : o.entrySet()) { profiles.authenticationDatabase.profiles.put( UUIDTypeAdapter.fromString(entry1.getKey()), GSON.fromJson(entry1.getValue(), OnlineProfile.class)); } } else { profiles.everythingElse.add(entry.getKey(), entry.getValue()); } } INSTANCE = profiles; return INSTANCE; } finally { if (INSTANCE == null) { INSTANCE = new LauncherProfiles(); } INSTANCE.markLoaded(); } } private static File getFile() { File mcDataDir = DevLauncher.workingDirectory; if (!mcDataDir.exists()) { mcDataDir.mkdirs(); } return new File(mcDataDir, "launcher_profiles.json"); } private final JsonObject everythingElse = new JsonObject(); private String clientToken = UUID.randomUUID().toString(); private final AuthDatabase authenticationDatabase = new AuthDatabase(); private transient UserFactory USERS; private void markLoaded() { USERS = new UserFactory(clientToken); } public UserFactory getUserFactory() { return USERS; } public String getClientToken() { return clientToken; } public void addProfile(OnlineProfile profile) { authenticationDatabase.profiles.put(profile.getUUID(), profile); modified(); } public void removeProfile(OnlineProfile profile) { authenticationDatabase.profiles.remove(profile.getUUID()); modified(); } public Iterable<OnlineProfile> getProfiles() { return new Iterable<OnlineProfile>() { @Override public Iterator<OnlineProfile> iterator() { final Iterator<OnlineProfile> real = authenticationDatabase.profiles.values().iterator(); return new Iterator<OnlineProfile>() { @Override public void remove() { real.remove(); modified(); } @Override public OnlineProfile next() { return real.next(); } @Override public boolean hasNext() { return real.hasNext(); } }; } }; } private static final Gson GSON = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); private static final class AuthDatabase { private final Map<UUID, OnlineProfile> profiles = new HashMap<>(); } }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // start parse param from request String usersListFilePath = request.getParameter("usersListFilePath"); LOGGER.info("usersListFilePath=" + usersListFilePath); List<Long> usersToRank = parseUsersIdsFromFile(usersListFilePath); Date fromDate = getFromDate(request); LOGGER.info("fromDate=" + fromDate); Date toDate = getToDate(request); LOGGER.info("toDate=" + toDate); List<String> topicTweets = getTopicTweets(request); LOGGER.info("In Topic Tweets:"); int count = 0; for (String tweet : topicTweets) { int i = count++; LOGGER.debug(i + ") " + tweet); } List<String> outOfTopicTweets = getOutOfTopicTweets(request); LOGGER.info("Out of Topic Tweets:"); count = 0; for (String tweet : outOfTopicTweets) { int i = count++; LOGGER.debug(i + ") " + tweet); } int tweetsPerDocument = getTweetsPerDocument(request); LOGGER.info("tweetsPerDocument=" + tweetsPerDocument); int k = getK(request); LOGGER.info("k=" + k); // end parse param from request // build the ranking calculator final TopicScorer topicScorer = initKnnTopicScorer(topicTweets, outOfTopicTweets, k); final RankingCalculator rankingCalculator = new RankingCalculator(this.twitterCache, topicScorer); // use it final List<RankedUser> rankedUsers = rankingCalculator.getRankedUsersWithoutUrlsResolution(usersToRank, fromDate, toDate); LOGGER.info("found users: " + rankedUsers.size()); final List<InfluenceUser> influenceUsers = new LinkedList<InfluenceUser>(); for (RankedUser currentUser : rankedUsers) { final String screenName = currentUser.getScreenName(); LOGGER.info("loading user by screen name '" + screenName + "'"); final User fromPersistence = loadUserByScreenName(screenName); final InfluenceUser influencer = new InfluenceUser(currentUser, fromPersistence); influenceUsers.add(influencer); } // serialize in json final String influencersAsJson = this.gson.toJson(influenceUsers, new TypeToken<List<InfluenceUser>>() {}.getType()); // write results to file final String influencersFilename = "influencers-" + UUID.randomUUID().toString() + ".json"; final String influencersFilePath = this.pl.getInfluencersResultsDirectory() + influencersFilename; LOGGER.info("writing rankedUsers '" + influencersAsJson + "' to file " + influencersFilePath); LOGGER.info("influencers to be serialized in json " + influenceUsers.size()); try { writeStringToFile(influencersFilePath, influencersAsJson); } catch (IOException e) { throw new ServletException( "cant write to file '" + influencersFilePath + "' these contents: '" + influencersAsJson + "'", e); } LOGGER.info("wrote file: '" + influencersFilePath + "' - dispatching to view"); request.setAttribute("influencersFilePath", influencersFilePath); final RequestDispatcher requestDispatcher = request.getRequestDispatcher("influencers-result.jsp"); requestDispatcher.forward(request, response); }