List of usage examples for com.mongodb BasicDBObject getString
public String getString(final String key)
From source file:com.ikanow.infinit.e.api.knowledge.processing.ScoringUtils.java
License:Open Source License
private void stage4_prepareEntsForOutput(LinkedList<BasicDBObject> entityReturn) { if (_s0_nNumEntsReturn > 0) { // (else entities not enabled) for (EntSigHolder qsf = _s3_pqEnt.poll(); null != qsf; qsf = _s3_pqEnt.poll()) // (start with lowest ranking) {// ww w .j a v a2 s .c om BasicDBObject ent = qsf.unusedDbo; if (null == ent) { int nTries = 0; if (null != qsf.entityInstances) { // (should never be null but just to be on the safe side... for (TempEntityInDocBucket tefb : qsf.entityInstances) { // (Try to find an entity that wasn't promoted ie can now be re-used // if we can't find one quite quickly then bail out and we'll pay the cost of cloning it) if (!tefb.doc.bPromoted) { ent = tefb.dbo; break; } else if (++nTries > 10) { break; } } if (null == ent) { ent = qsf.entityInstances.get(0).dbo; } } else { // (no entityInstances, something alias-related has gone wrong, just skip) continue; } } //TESTED qsf.entityInstances = null; // (don't need this any more, can be gc'd) try { if (null != qsf.aliasInfo) { if (!qsf.index.equals(qsf.aliasInfo.getIndex())) { ent.put(EntityPojo.index_, qsf.aliasInfo.getIndex()); ent.put(EntityPojo.disambiguated_name_, qsf.aliasInfo.getDisambiguatedName()); ent.put(EntityPojo.type_, qsf.aliasInfo.getType()); ent.put(EntityPojo.dimension_, qsf.aliasInfo.getDimension()); if (null != qsf.aliasInfo.getGeotag()) { BasicDBObject aliasedGeoTag = new BasicDBObject(); aliasedGeoTag.put(GeoPojo.lat_, qsf.aliasInfo.getGeotag().lat); aliasedGeoTag.put(GeoPojo.lon_, qsf.aliasInfo.getGeotag().lon); ent.put(EntityPojo.geotag_, aliasedGeoTag); if (null != qsf.aliasInfo.getOntology_type()) { ent.put(EntityPojo.ontology_type_, qsf.aliasInfo.getOntology_type()); } } //TESTED } } //TESTED if (null == ent.get(EntityPojo.datasetSignificance_)) { // Not getting promoted so need to add fields... if (Double.isNaN(qsf.datasetSignificance)) { ent.put("datasetSignificance", 0.0); } else { ent.put(EntityPojo.datasetSignificance_, qsf.datasetSignificance); } ent.put(EntityPojo.queryCoverage_, qsf.queryCoverage); ent.put(EntityPojo.averageFreq_, qsf.avgFreqOverQuerySubset); if (qsf.nTotalSentimentValues > 0) { ent.put(EntityPojo.positiveSentiment_, qsf.positiveSentiment); ent.put(EntityPojo.negativeSentiment_, qsf.negativeSentiment); ent.put(EntityPojo.sentimentCount_, qsf.nTotalSentimentValues); } } else { // (... but can just use it without cloning) BasicDBObject ent2 = new BasicDBObject(); for (Map.Entry<String, Object> kv : ent.entrySet()) { ent2.append(kv.getKey(), kv.getValue()); } ent = ent2; } ent.removeField(EntityPojo.relevance_); if (Double.isNaN(qsf.maxDocSig)) { ent.put(EntityPojo.significance_, 0.0); } else { ent.put(EntityPojo.significance_, qsf.maxDocSig); } ent.put(EntityPojo.frequency_, (long) qsf.maxFreq); entityReturn.addFirst(ent); } catch (Exception e) { // Probably a JSON error, just carry on String title = ent.getString(EntityPojo.index_); logger.error(title + ": " + e.getMessage()); } //TESTED } } //TESTED }
From source file:com.ikanow.infinit.e.api.knowledge.processing.ScoringUtils.java
License:Open Source License
private double getManualScoreWeights(AdvancedQueryPojo.QueryScorePojo scoreParams, BasicDBObject doc) { // Highest prio: source key weight if (null != scoreParams.sourceWeights) { String sourceKey = DocumentPojo.getSourceKey(doc.getString(DocumentPojo.sourceKey_)); Double dWeight = scoreParams.sourceWeights.get(sourceKey); if (null != dWeight) { return dWeight; }/*from ww w . j a v a 2 s. co m*/ } // Middle prio: type if (null != scoreParams.typeWeights) { String mediaType = doc.getString(DocumentPojo.mediaType_); Double dWeight = scoreParams.typeWeights.get(mediaType); if (null != dWeight) { return dWeight; } } // Lowest prio: average of tags if (null != scoreParams.tagWeights) { double dScore = 0.0; int nComps = 0; BasicDBList tags = (BasicDBList) doc.get(DocumentPojo.tags_); if (null != tags) { for (Object tagObj : tags) { String tag = (String) tagObj; Double dWeight = scoreParams.tagWeights.get(tag); if (null != dWeight) { nComps++; dScore += dWeight; } } if (nComps > 0) { return dScore / nComps; } } } return 1.0; }
From source file:com.ikanow.infinit.e.api.knowledge.processing.ScoringUtils_Associations.java
License:Open Source License
static void addStandaloneEvents(BasicDBObject doc, double dDocSig, int nPhase, StandaloneEventHashAggregator standaloneEventAggregator, boolean bEntTypeFilterPositive, boolean bAssocVerbFilterPositive, HashSet<String> entTypeFilter, HashSet<String> assocVerbFilter, boolean bEvents, boolean bSummaries, boolean bFacts) { if (standaloneEventAggregator.bSimulateAggregation) { bSummaries = false;/*from ww w . j av a 2 s . c o m*/ } String sDocIsoPubDate = null; BasicDBList lev = (BasicDBList) (doc.get(DocumentPojo.associations_)); if (null != lev) { for (Iterator<?> e0 = lev.iterator(); e0.hasNext();) { BasicDBObject e = (BasicDBObject) e0.next(); String sEvType = e.getString(AssociationPojo.assoc_type_); boolean bIsFact = false; boolean bIsSummary = false; boolean bKeep = true; if (null == sEvType) { bKeep = false; } else if (sEvType.equalsIgnoreCase("event")) { if (!bEvents) bKeep = false; } else if (sEvType.equalsIgnoreCase("fact")) { if (!bFacts) bKeep = false; bIsFact = true; } else if (sEvType.equalsIgnoreCase("summary")) { if (!bSummaries) bKeep = false; bIsSummary = true; } //TESTED x4 // Filter and aliasing logic: if (bKeep) { boolean bKeep2 = filterAndAliasAssociation(e, standaloneEventAggregator.aliasLookup, true, bEntTypeFilterPositive, bAssocVerbFilterPositive, entTypeFilter, assocVerbFilter); if (!bKeep2) { e0.remove(); // (remove/rename events based on filters where we can, // means we don't have to do it in stage4) bKeep = false; } } //TESTED if (bKeep) { String time_start = null; String time_end = null; // (normally not needed) if (!standaloneEventAggregator.bSimulateAggregation) { //else times are discarded // Add time from document time_start = e.getString(AssociationPojo.time_start_); if (null == time_start) { if (null == sDocIsoPubDate) { // Convert docu pub date to ISO (day granularity): Date pubDate = (Date) doc.get(DocumentPojo.publishedDate_); if (null != pubDate) { SimpleDateFormat f2 = new SimpleDateFormat("yyyy-MM-dd"); time_start = f2.format(pubDate); } } else { time_start = sDocIsoPubDate; // (so it doesn't get added again below) } } //TESTED else { // Remove hourly granularity for consistency time_start = time_start.replaceAll("T.*$", ""); time_end = e.getString(AssociationPojo.time_end_); if (null != time_end) { time_end = time_end.replaceAll("T.*$", ""); } } //TESTED (with debug code, eg time_start = "1997-07-16T19:20:30+01:00") if (null != time_start) { // Ensure it has day granularity, to help with aggregation e.put(AssociationPojo.time_start_, time_start); if (null != time_end) { e.put(AssociationPojo.time_end_, time_end); } } //TESTED } //(end if normal standalone mode, not aggregation simulation) StandaloneEventHashCode evtHolder = new StandaloneEventHashCode( standaloneEventAggregator.bSimulateAggregation, e, bIsSummary, bIsFact); BasicDBObject oldEvt = standaloneEventAggregator.store.get(evtHolder); if (null == oldEvt) { // Doc count (see below) e.put(AssociationPojo.doccount_, 1); double dAssocSig = dDocSig * dDocSig; // Weight down summaries slightly (80%), and summaries with missing entities a lot (50%) if (bIsSummary) { String sEntity2 = (String) e.get(AssociationPojo.entity2_); if (null == sEntity2) { dAssocSig *= 0.50; } else { dAssocSig *= 0.80; } } // Running significance count: e.put(AssociationPojo.assoc_sig_, dAssocSig); // (use sum-squared to score up events that occur frequently) if (dAssocSig > standaloneEventAggregator.dMaxSig) { standaloneEventAggregator.dMaxSig = dAssocSig; } standaloneEventAggregator.store.put(evtHolder, e); // Add to list in some sort of very basic order... if (2 == nPhase) { // Put at the back, it's probably really low sig standaloneEventAggregator.tmpList.add(e); } else if (1 == nPhase) { // Put at the front until Phase 0 comes along standaloneEventAggregator.tmpList.addFirst(e); standaloneEventAggregator.nPhase1Events++; } else { // phases 0 and 1 get the higher orderings standaloneEventAggregator.tmpList.addFirst(e); standaloneEventAggregator.nPhase0Events++; } } else { // Update doc count long nDocCount = oldEvt.getInt(AssociationPojo.doccount_, 1) + 1; oldEvt.put(AssociationPojo.doccount_, nDocCount); // Running significance count: double dAssocSig = oldEvt.getDouble(AssociationPojo.doccount_) + dDocSig * dDocSig; oldEvt.put(AssociationPojo.assoc_sig_, dAssocSig); if (dAssocSig / nDocCount > standaloneEventAggregator.dMaxSig) { standaloneEventAggregator.dMaxSig = dAssocSig; } if (bIsFact && !standaloneEventAggregator.bSimulateAggregation) { // For facts, also update the time range: String old_time_start = oldEvt.getString(AssociationPojo.time_start_); String old_time_end = oldEvt.getString(AssociationPojo.time_end_); // Just keep this really simple and inefficient: TreeSet<String> timeOrder = new TreeSet<String>(); if (null != old_time_start) { timeOrder.add(old_time_start); } if (null != old_time_end) { timeOrder.add(old_time_end); } if (null != time_start) { timeOrder.add(time_start); } if (null != time_end) { timeOrder.add(time_end); } if (timeOrder.size() > 1) { Iterator<String> itStart = timeOrder.iterator(); oldEvt.put(AssociationPojo.time_start_, itStart.next()); Iterator<String> itEnd = timeOrder.descendingIterator(); oldEvt.put(AssociationPojo.time_end_, itEnd.next()); } } // end if is fact - treat times different } //TESTED } // (end if keeping this event) } // (end loop over events) } // (end if this doc has events) }
From source file:com.ikanow.infinit.e.api.knowledge.processing.ScoringUtils_Associations.java
License:Open Source License
static boolean filterAndAliasAssociation(BasicDBObject e, AliasLookupTable aliasLookup, boolean bModifyAssocObj, boolean bEntTypeFilterPositive, boolean bAssocVerbFilterPositive, HashSet<String> entTypeFilter, HashSet<String> assocVerbFilter) { // Verb filter if (null != assocVerbFilter) { if (bAssocVerbFilterPositive) { if (!assocVerbFilter.contains(e.getString(AssociationPojo.verb_category_))) { return false; }//from w w w. j a v a 2 s . co m } else if (assocVerbFilter.contains(e.getString(AssociationPojo.verb_category_))) { return false; } } //TESTED if ((null != entTypeFilter) || (null != aliasLookup)) { String ent1Index = e.getString(AssociationPojo.entity1_index_); if (null != ent1Index) { String entType = null; if (null != aliasLookup) { EntityFeaturePojo alias = aliasLookup.getAliasMaster(ent1Index); if (null != alias) { ent1Index = alias.getIndex(); entType = alias.getType(); if (ent1Index.equalsIgnoreCase("discard")) { return false; } else { // rename if (bModifyAssocObj) { e.put(AssociationPojo.entity1_index_, alias.getIndex()); } } } } //TESTED if (null == entType) { int nIndex = ent1Index.lastIndexOf('/'); if (nIndex >= 0) { entType = ent1Index.substring(nIndex + 1); } } else { entType = entType.toLowerCase(); } //TESTED (both clauses) if (null != entTypeFilter) { if (bEntTypeFilterPositive) { if ((null != entType) && (!entTypeFilter.contains(entType))) { return false; } } else if ((null != entType) && (entTypeFilter.contains(entType))) { return false; } } //TESTED } //(end if ent1_index exists) String ent2Index = e.getString(AssociationPojo.entity2_index_); if (null != ent2Index) { String entType = null; if (null != aliasLookup) { EntityFeaturePojo alias = aliasLookup.getAliasMaster(ent2Index); if (null != alias) { ent2Index = alias.getIndex(); entType = alias.getType(); if (ent2Index.equalsIgnoreCase("discard")) { return false; } else { // rename if (bModifyAssocObj) { e.put(AssociationPojo.entity2_index_, alias.getIndex()); } } } } //TESTED (cut and paste from ent1) if (null == entType) { int nIndex = ent2Index.lastIndexOf('/'); if (nIndex >= 0) { entType = ent2Index.substring(nIndex + 1); } } else { entType = entType.toLowerCase(); } //TESTED (cut and paste from ent1) if (null != entTypeFilter) { if (bEntTypeFilterPositive) { if ((null != entType) && (!entTypeFilter.contains(entType))) { return false; } } else if ((null != entType) && (entTypeFilter.contains(entType))) { return false; } } } //(end if ent2_index exists) String geoIndex = e.getString(AssociationPojo.geo_index_); if (null != geoIndex) { String entType = null; if (null != aliasLookup) { EntityFeaturePojo alias = aliasLookup.getAliasMaster(geoIndex); if (null != alias) { geoIndex = alias.getIndex(); entType = alias.getType(); if (geoIndex.equalsIgnoreCase("discard")) { if ((ent1Index == null) || (ent2Index == null)) { return false; } else if (bModifyAssocObj) { e.remove(AssociationPojo.geo_index_); } else { return false; } } else { // rename if (bModifyAssocObj) { e.put(AssociationPojo.geo_index_, alias.getIndex()); } } } } //TESTED (cut and paste from ent1) if (null == entType) { int nIndex = geoIndex.lastIndexOf('/'); if (nIndex >= 0) { entType = geoIndex.substring(nIndex + 1); } } else { entType = entType.toLowerCase(); } //TESTED (cut and paste from ent1) if (null != entTypeFilter) { if (bEntTypeFilterPositive) { if ((null != entType) && (!entTypeFilter.contains(entType))) { return false; } } else if ((null != entType) && (entTypeFilter.contains(entType))) { return false; } } } //(end if ent2_index exists) } //(end entity filter logic for associations) //TESTED return true; }
From source file:com.ikanow.infinit.e.api.knowledge.processing.ScoringUtils_MultiCommunity.java
License:Open Source License
static boolean community_areDuplicates(TempDocBucket lhs, TempDocBucket rhs) { if (lhs.url.equals(rhs.url)) { // These are probably duplicates, we'll do some more tests... BasicDBList lhsEnts = (BasicDBList) lhs.dbo.get(DocumentPojo.entities_); BasicDBList rhsEnts = (BasicDBList) rhs.dbo.get(DocumentPojo.entities_); if (!((null != lhsEnts) ^ (null != rhsEnts))) // ie both null or neither null {/*from w w w . j av a 2 s. c o m*/ if (null != lhsEnts) { if (lhsEnts.size() != rhsEnts.size()) { return false; } //TESTED if (lhsEnts.size() > 0) { BasicDBObject lhsFirstEnt = (BasicDBObject) lhsEnts.get(0); BasicDBObject rhsFirstEnt = (BasicDBObject) rhsEnts.get(0); String lhsIndex = lhsFirstEnt.getString(EntityPojo.index_); String rhsIndex = rhsFirstEnt.getString(EntityPojo.index_); if ((null != lhsIndex) && (null != rhsIndex)) { if (!lhsIndex.equals(rhsIndex)) { return false; } } } //(end "random" entity test) //TESTED } } // (end entity count test) // Finally we'll just count the events: BasicDBList lhsEvents = (BasicDBList) lhs.dbo.get(DocumentPojo.associations_); BasicDBList rhsEvents = (BasicDBList) rhs.dbo.get(DocumentPojo.associations_); if (!((null != lhsEvents) ^ (null != rhsEvents))) // ie both null or neither null { if (null != lhsEvents) { if (lhsEvents.size() != rhsEvents.size()) { return false; } //TESTED } } //(end event count test) return true; } return false; }
From source file:com.ikanow.infinit.e.api.social.community.CommunityHandler.java
License:Open Source License
/** * updateMemberStatus (REST)//from w w w . j a v a 2 s .co m */ public ResponsePojo updateMemberStatus(String callerIdStr, String personIdStr, String communityIdStr, String userStatus, CommunityPojo.CommunityType communityType) { boolean isSysAdmin = RESTTools.adminLookup(callerIdStr); ResponsePojo rp = new ResponsePojo(); try { communityIdStr = allowCommunityRegex(callerIdStr, communityIdStr); //verify user is in this community, then update status BasicDBObject query = new BasicDBObject("_id", new ObjectId(communityIdStr)); DBObject dbo = DbManager.getSocial().getCommunity().findOne(query); if (dbo != null) { // PersonId can be _id or username/email BasicDBObject dboPerson = (BasicDBObject) DbManager.getSocial().getPerson() .findOne(new BasicDBObject("email", personIdStr)); if (null != dboPerson) { // (ie personId isn't an email address... convert to ObjectId and try again) personIdStr = dboPerson.getString("_id"); } // OK from here on, personId is the object Id... boolean bAuthorized = isSysAdmin || SocialUtils.isOwnerOrModerator(communityIdStr, callerIdStr) || isRemovingSelf(userStatus, callerIdStr, personIdStr); if (bAuthorized) { CommunityPojo cp = CommunityPojo.fromDb(dbo, CommunityPojo.class); ObjectId personId = new ObjectId(personIdStr); if (cp.isOwner(personId) && !userStatus.equalsIgnoreCase("active")) { rp.setResponse(new ResponseObject("Update member status", false, "Can't change owner status, remove their ownership first")); return rp; } //TESTED (tried as owner+admin (failed both times), tested owner works fine if setting/keeping active) else if (cp.isMember(personId)) { // Remove user: if (userStatus.equalsIgnoreCase("remove")) { //removeCommunityMember(callerIdStr, communityIdStr, personIdStr); rp = removeCommunityMember(callerIdStr, communityIdStr, personIdStr); //.setResponse(new ResponseObject("Update member status",true,"Member removed from community.")); } else { //verified user, update status if (cp.updateMemberStatus(personIdStr, userStatus)) { ///////////////////////////////////////////////////////////////////////////////////////////////// // TODO (INF-1214): Make this code more robust to handle changes to the community that need to // Caleb: this means change update to $set ///////////////////////////////////////////////////////////////////////////////////////////////// DbManager.getSocial().getCommunity().update(query, cp.toDb()); rp.setResponse(new ResponseObject("Update member status", true, "Updated member status successfully")); } else { rp.setResponse(new ResponseObject("Update member status", false, "Failed to update status")); } } } else { rp.setResponse(new ResponseObject("Update member status", false, "User was not a member of the community")); } } else { rp.setResponse(new ResponseObject("Update member status", false, "Caller must be admin, or a community owner or moderator")); } //TESTED - tried to update my status as member (failed), as admin (succeeded), as moderator (succeeded), as owner (succeeded) } else { rp.setResponse(new ResponseObject("Update member status", false, "Community does not exist")); } } catch (Exception ex) { rp.setResponse(new ResponseObject("Update member status", false, Globals .populateStackTrace(new StringBuffer("General Error, bad params maybe? "), ex).toString())); } return rp; }
From source file:com.ikanow.infinit.e.api.social.community.CommunityHandler.java
License:Open Source License
/** * updateMemberType (REST)//w w w .ja v a 2s .c om */ public ResponsePojo updateMemberType(String callerIdStr, String personIdStr, String communityIdStr, String userType, CommunityPojo.CommunityType communityType) { boolean isSysAdmin = RESTTools.adminLookup(callerIdStr); ResponsePojo rp = new ResponsePojo(); try { if (!userType.equalsIgnoreCase("owner") && !userType.equalsIgnoreCase("moderator") && !userType.equalsIgnoreCase("member") && !userType.equalsIgnoreCase("content_publisher")) { rp.setResponse(new ResponseObject("Update member type", false, "Invalid user type: " + userType)); return rp; } //TESTED - tested all the types work, hacked members.jsp to insert invalid type //verify user is in this community, then update status communityIdStr = allowCommunityRegex(callerIdStr, communityIdStr); BasicDBObject query = new BasicDBObject("_id", new ObjectId(communityIdStr)); DBObject dbo = DbManager.getSocial().getCommunity().findOne(query); if (dbo != null) { // PersonId can be _id or username/email BasicDBObject dboPerson = (BasicDBObject) DbManager.getSocial().getPerson() .findOne(new BasicDBObject("email", personIdStr)); if (null != dboPerson) { // (ie personId isn't an email address... convert to ObjectId and try again) personIdStr = dboPerson.getString("_id"); } // OK from here on, personId is the object Id... CommunityPojo cp = CommunityPojo.fromDb(dbo, CommunityPojo.class); boolean bOwnershipChangeRequested = userType.equalsIgnoreCase("owner"); boolean bAuthorized = isSysAdmin; if (!bAuthorized) { if (bOwnershipChangeRequested) { // must be owner or admin bAuthorized = cp.isOwner(new ObjectId(callerIdStr)); } //TESTED - tried to update myself as moderator to owner (failed), gave up my community (succeeded), changed ownership as admin (FAILED) else { // Can also be moderator bAuthorized = SocialUtils.isOwnerOrModerator(communityIdStr, callerIdStr); } //TESTED - tried to update my role as member (failed), as admin->moderator (succeeded), as moderator (succeeded) } if (bAuthorized) // (see above) { if (cp.isMember(new ObjectId(personIdStr))) { boolean bChangedMembership = false; boolean bChangedOwnership = !bOwnershipChangeRequested; ObjectId personId = new ObjectId(personIdStr); // Check that not trying to downgrade owner... if (cp.isOwner(personId) && !bOwnershipChangeRequested) { rp.setResponse(new ResponseObject("Update member type", false, "To change ownership, set new owner, will automatically downgrade existing owner to moderator")); return rp; } //TESTED String personDisplayName = null; //verified user, update status for (CommunityMemberPojo cmp : cp.getMembers()) { if (cmp.get_id().equals(personId)) { if ((MemberType.user_group == cmp.getType()) && bOwnershipChangeRequested) { // Not allowed to set a user group to be the owner (obv) rp.setResponse(new ResponseObject("Update member type", false, "Can't set a user group to be the owner")); return rp; } //TESTED cmp.setUserType(userType); personDisplayName = cmp.getDisplayName(); bChangedMembership = true; if (bChangedOwnership) { // (includes case where didn't need to) break; } } //TESTED if (bOwnershipChangeRequested && cmp.get_id().equals(cp.getOwnerId())) { cmp.setUserType("moderator"); bChangedOwnership = true; if (bChangedMembership) { break; } } //TESTED } if (bChangedMembership) { if (bOwnershipChangeRequested) { cp.setOwnerId(personId); cp.setOwnerDisplayName(personDisplayName); } //TESTED ///////////////////////////////////////////////////////////////////////////////////////////////// // TODO (INF-1214): Make this code more robust to handle changes to the community that need to // Caleb: this means change update to $set ///////////////////////////////////////////////////////////////////////////////////////////////// DbManager.getSocial().getCommunity().update(query, cp.toDb()); rp.setResponse(new ResponseObject("Update member type", true, "Updated member type successfully")); } //TESTED } else { rp.setResponse(new ResponseObject("Update member type", false, "User was not a member of the community")); } } else { rp.setResponse(new ResponseObject("Update member type", false, "Caller must be admin/owner/moderator (unless changing ownership)")); } } else { rp.setResponse(new ResponseObject("Update member type", false, "Community does not exist")); } } catch (Exception ex) { rp.setResponse(new ResponseObject("Update member type", false, Globals .populateStackTrace(new StringBuffer("General Error, bad params maybe? "), ex).toString())); } return rp; }
From source file:com.ikanow.infinit.e.api.social.community.CommunityHandler.java
License:Open Source License
/** * inviteCommunity (REST)//w w w . j av a 2 s . c o m * Invite a user to a community, only add them as pending to community * and do not add community into the person object yet * Need to send email out // (Note supports personId as either Id or username (email) both are unique indexes) * @param personIdStr * @param userIdStr * @param communityIdStr * @return */ public ResponsePojo inviteCommunity(String userIdStr, String personIdStr, String communityIdStr, String skipInvitation, CommunityPojo.CommunityType communityType) { ResponsePojo rp = new ResponsePojo(); try { communityIdStr = allowCommunityRegex(userIdStr, communityIdStr); } catch (Exception e) { rp.setResponse(new ResponseObject("Invite Community", false, "Error returning community info: " + e.getMessage())); return rp; } boolean skipInvite = ((null != skipInvitation) && (skipInvitation.equalsIgnoreCase("true"))) ? true : false; ///////////////////////////////////////////////////////////////////////////////////////////////// // Note: Only Sys Admins, Community Owner, and Community Moderators can invite users to // private communities however any member can be able to invite someone to a public community boolean isOwnerOrModerator = SocialUtils.isOwnerOrModerator(communityIdStr, userIdStr); boolean isSysAdmin = RESTTools.adminLookup(userIdStr); boolean canInvite = (isOwnerOrModerator || isSysAdmin) ? true : false; try { BasicDBObject query = new BasicDBObject("_id", new ObjectId(communityIdStr)); DBObject dboComm = DbManager.getSocial().getCommunity().findOne(query); if (dboComm != null) { CommunityPojo cp = CommunityPojo.fromDb(dboComm, CommunityPojo.class); // Make sure this isn't a personal community if (!cp.getIsPersonalCommunity()) { // Check to see if the user has permissions to invite or selfregister boolean selfRegister = canSelfRegister(cp); if (canInvite || cp.getOwnerId().toString().equalsIgnoreCase(userIdStr) || selfRegister) { BasicDBObject dboPerson = (BasicDBObject) DbManager.getSocial().getPerson() .findOne(new BasicDBObject("email", personIdStr)); if (null == dboPerson) { // (ie personId isn't an email address... convert to ObjectId and try again) dboPerson = (BasicDBObject) DbManager.getSocial().getPerson() .findOne(new BasicDBObject("_id", new ObjectId(personIdStr))); } else { personIdStr = dboPerson.getString("_id"); } ObjectId personOrUserGroupId = null; PersonPojo pp = null; CommunityPojo userGroup = null; if (null == dboPerson) { userGroup = CommunityPojo.fromDb( DbManager.getSocial().getCommunity() .findOne(new BasicDBObject("_id", new ObjectId(personIdStr))), CommunityPojo.class); if (null != userGroup) personOrUserGroupId = userGroup.getId(); if ((null != userGroup) && (CommunityType.user != userGroup.getType())) { rp.setResponse(new ResponseObject("Join member to community", false, "Can't add data groups as members.")); return rp; } if ((null != userGroup) && (CommunityType.user == cp.getType())) { rp.setResponse(new ResponseObject("Join member to community", false, "Can't add user groups to user groups.")); return rp; } } else { pp = PersonPojo.fromDb(dboPerson, PersonPojo.class); if (null != pp) personOrUserGroupId = pp.get_id(); } // OK from here on, personIdStr is the object Id... if ((pp != null) || (userGroup != null)) { //need to check for if a person is pending, and skipInvite and isSysAdmin, otherwise //they would just get sent an email again, so leave it be boolean isPending = false; if (isSysAdmin && skipInvite) { isPending = isMemberPending(cp, personOrUserGroupId); } if (selfRegister) { //If the comm allows for self registering, just call join community //instead of invite, it will handle registration return this.joinCommunity(personOrUserGroupId.toString(), communityIdStr, isSysAdmin, communityType); } else if (!cp.isMember(personOrUserGroupId) || isPending) { if (isSysAdmin && skipInvite) // Can only skip invite if user is Admin { // Update community with new member cp.addMember(personOrUserGroupId, pp, userGroup, false); // Member status set to Active ///////////////////////////////////////////////////////////////////////////////////////////////// // TODO (INF-1214): Make this code more robust to handle changes to the community that need to // Caleb: this means change update to $set ///////////////////////////////////////////////////////////////////////////////////////////////// DbManager.getSocial().getCommunity().update(query, cp.toDb()); ///////////////////////////////////////////////////////////////////////////////////////////////// // TODO (INF-1214): Make this code more robust to handle changes to the community that need to // Caleb: this means change update to $set ///////////////////////////////////////////////////////////////////////////////////////////////// if (null != pp) { // Add community to persons object and save to db pp.addCommunity(cp); DbManager.getSocial().getPerson() .update(new BasicDBObject("_id", pp.get_id()), pp.toDb()); rp.setResponse(new ResponseObject("Invite Community", true, "User added to community successfully.")); } else { //TODO (INF-2866): might need to do some person manipulation rp.setResponse(new ResponseObject("Invite Community", true, "User group added to community successfully.")); } } else { cp.addMember(personOrUserGroupId, pp, userGroup, true); // Member status set to Pending ///////////////////////////////////////////////////////////////////////////////////////////////// // TODO (INF-1214): Make this code more robust to handle changes to the community that need to // Caleb: this means change update to $set ///////////////////////////////////////////////////////////////////////////////////////////////// DbManager.getSocial().getCommunity().update(query, cp.toDb()); //send email out inviting user CommunityApprovePojo cap = cp.createCommunityApprove( RESTTools.generateRandomId(), personIdStr, communityIdStr, "invite", userIdStr); DbManager.getSocial().getCommunityApprove().insert(cap.toDb()); PropertiesManager propManager = new PropertiesManager(); String rootUrl = propManager.getUrlRoot(); if (null == rootUrl) { rp.setResponse(new ResponseObject("Invite Community", false, "The system was unable to email the invite because an invite was required and root.url is not set up.")); return rp; } String subject = "Invite to join infinit.e community: " + cp.getName(); String body = "You have been invited to join the community " + cp.getName() + "<br/><a href=\"" + rootUrl + "social/community/requestresponse/" + cap.get_id().toString() + "/true\">Accept</a> " + "<a href=\"" + rootUrl + "social/community/requestresponse/" + cap.get_id().toString() + "/false\">Deny</a>"; SendMail mail = null; if (null != pp) { mail = new SendMail(new PropertiesManager().getAdminEmailAddress(), pp.getEmail(), subject, body); } else { mail = new SendMail(new PropertiesManager().getAdminEmailAddress(), getToAddressesFromCommunity(userGroup), subject, body); } if (mail.send("text/html")) { if (isSysAdmin) { if (null == pp) rp.setResponse(new ResponseObject("Invite Community", true, "Invited user to community successfully: " + cap.get_id().toString())); else rp.setResponse(new ResponseObject("Invite Community", true, "Invited user group to community successfully: " + cap.get_id().toString())); } else { if (null == pp) rp.setResponse(new ResponseObject("Invite Community", true, "Invited user to community successfully")); else rp.setResponse(new ResponseObject("Invite Community", true, "Invited user group to community successfully")); } } else { rp.setResponse(new ResponseObject("Invite Community", false, "The system was unable to email the invite for an unknown reason (eg an invite was required and the mail server is not setup).")); } } } else { //otherwise just return we failed rp.setResponse(new ResponseObject("Invite Community", false, "The user is already a member of this community.")); } } else { rp.setResponse(new ResponseObject("Invite Community", false, "Person/User Group does not exist")); } } else { rp.setResponse(new ResponseObject("Invite Community", false, "You must be owner to invite other members, if you received an invite, you must accept it through that")); } } else { rp.setResponse(new ResponseObject("Invite Community", false, "Cannot invite members to personal community")); } } else { rp.setResponse(new ResponseObject("Invite Community", false, "Community does not exist")); } } catch (Exception ex) { rp.setResponse(new ResponseObject("Invite Community", false, Globals .populateStackTrace(new StringBuffer("General Error, bad params maybe? "), ex).toString())); } return rp; }
From source file:com.ikanow.infinit.e.api.social.sharing.ShareHandler.java
License:Open Source License
private String getReferenceString(SharePojo share) { // FILE:/*from w ww. j ava 2s . c o m*/ if (null == share.getDocumentLocation().get_id()) { // local file based reference FileInputStream fin = null; Scanner s = null; try { File f = new File(share.getDocumentLocation().getCollection()); fin = new FileInputStream(f); s = new Scanner(fin, "UTF-8"); return (s.useDelimiter("\n").next()); } catch (Exception e) { return null; } finally { try { if (null != fin) fin.close(); if (null != s) s.close(); } catch (Exception e) { } // (probably just never opened) } } // DB: // Carry on, this is a database object HashSet<String> shareIdStrs = new HashSet<String>(); for (ShareCommunityPojo commIds : share.getCommunities()) { shareIdStrs.add(commIds.get_id().toString()); } String retVal = null; BasicDBObject query = new BasicDBObject(DocumentPojo._id_, share.getDocumentLocation().get_id()); // (same for all artifacts) String dbName = share.getDocumentLocation().getDatabase(); String collectionName = share.getDocumentLocation().getCollection(); BasicDBObject returnVal = (BasicDBObject) MongoDbManager.getCollection(dbName, collectionName) .findOne(query); try { BasicDBList communities = null; boolean bCustomJob = dbName.equals("custommr"); // (a bit different) boolean bFoundOverlap = false; if (!bCustomJob) { ObjectId communityId = (ObjectId) returnVal.get(DocumentPojo.communityId_); // (same for other artifacts) bFoundOverlap = shareIdStrs.contains(communityId.toString()); } else { communities = (BasicDBList) returnVal.get("communityIds"); // (shared across multiple json types) for (Object commIdObj : communities) { ObjectId commId = (ObjectId) commIdObj; if (shareIdStrs.contains(commId.toString())) { bFoundOverlap = true; break; } } } if (!bFoundOverlap) { throw new RuntimeException(""); // (turned into the common message below) } if (!bCustomJob) { // everything but custom jobs Date modifiedTime = returnVal.getDate(DocumentPojo.modified_); // (same for other artifacts) if (null != modifiedTime) { share.setModified(modifiedTime); } retVal = returnVal.toString(); } else { // custom jobs String database = returnVal.getString(CustomMapReduceJobPojo.outputDatabase_); if (null == database) { database = dbName; } Date modifiedTime = returnVal.getDate(CustomMapReduceJobPojo.lastCompletionTime_); if (null != modifiedTime) { share.setModified(modifiedTime); } String collection = returnVal.getString(CustomMapReduceJobPojo.outputCollection_); BasicDBObject returnVal2 = (BasicDBObject) MongoDbManager.getCollection(database, collection) .findOne(); retVal = returnVal2.toString(); } } catch (Exception e) { throw new RuntimeException("Document not found or permission issue (no overlapping communities)"); } return retVal; }
From source file:com.ikanow.infinit.e.application.handlers.polls.LogstashSourceDeletionPollHandler.java
License:Apache License
@Override public void performPoll() { boolean isSlave = false; if (null == LOGSTASH_CONFIG) { // (static memory not yet initialized) try {/* w w w . ja v a2s .c o m*/ Thread.sleep(1000); // (extend the sleep time a bit) } catch (Exception e) { } return; } File logstashDirectory = new File(LOGSTASH_CONFIG); String slaveHostname = null; if (!logstashDirectory.isDirectory() || !logstashDirectory.canRead() || !logstashDirectory.canWrite()) { logstashDirectory = new File(LOGSTASH_CONFIG_DISTRIBUTED); isSlave = true; if (!logstashDirectory.isDirectory() || !logstashDirectory.canRead() || !logstashDirectory.canWrite()) { try { Thread.sleep(10000); // (extend the sleep time a bit) } catch (Exception e) { } return; } try { slaveHostname = java.net.InetAddress.getLocalHost().getHostName(); } catch (Exception e) { // too complex if we don't have a hostname, just return return; } } // Deletion of distributed sources requires some co-ordination, we'll do it in master if (isSlave) { // register my existence BasicDBObject existence = new BasicDBObject("_id", slaveHostname); existence.put("ping", new Date()); DbManager.getIngest().getLogHarvesterSlaves().save(existence); } //TESTED (by hand) else { // MASTER: clear out old slaves // (if it hasn't pinged for more than 30 minutes) long now = new Date().getTime(); BasicDBObject deadSlaveQuery = new BasicDBObject("ping", new BasicDBObject(DbManager.lt_, new Date(now - 1000L * 1800L))); boolean found = false; DBCursor dbc = DbManager.getIngest().getLogHarvesterSlaves().find(deadSlaveQuery); while (dbc.hasNext()) { BasicDBObject deadSlave = (BasicDBObject) dbc.next(); found = true; String hostname = deadSlave.getString("_id"); if (null != hostname) { DbManager.getIngest().getLogHarvesterQ().remove(new BasicDBObject("forSlave", hostname)); _logger.info("Removing unresponsive slave host=" + hostname); } } if (found) { DbManager.getIngest().getLogHarvesterSlaves().remove(deadSlaveQuery); } } //TESTED (by hand) // Read delete elements from the Q... if (null == _logHarvesterQ) { _logHarvesterQ = new MongoQueue(DbManager.getIngest().getLogHarvesterQ().getDB().getName(), DbManager.getIngest().getLogHarvesterQ().getName()); } BasicDBObject queueQuery = new BasicDBObject("deleteOnlyCommunityId", new BasicDBObject(DbManager.exists_, true)); if (!isSlave) { // only get master messages queueQuery.put("forSlave", new BasicDBObject(DbManager.exists_, false)); } else { // only get messages intended for me queueQuery.put("forSlave", slaveHostname); } DBObject nextElement = _logHarvesterQ.pop(queueQuery); LinkedList<TestLogstashExtractorPojo> secondaryQueue = new LinkedList<TestLogstashExtractorPojo>(); LinkedList<String> deleteAfterRestartQueue = new LinkedList<String>(); boolean deletedSources = false; boolean deletedSinceDbs = false; while (nextElement != null) { //DEBUG //System.out.println("HOST: " + slaveHostname + ": RECEIVED: " + nextElement.toString() + " FROM " + queueQuery); _logger.info("host=" + slaveHostname + " received=" + nextElement.toString() + " from=" + queueQuery); TestLogstashExtractorPojo testInfo = TestLogstashExtractorPojo.fromDb(nextElement, TestLogstashExtractorPojo.class); if (null == testInfo.sourceKey) { continue; // need a sourceKey parameter... } if (!isSlave) { // slaves don't need to delete anything from the index, only files secondaryQueue.add(testInfo); } //(end if master) try { // First off - need to remove the conf file and restart logstash if we're actually deleting this... boolean deletedSource = false; if ((null == testInfo.deleteDocsOnly) || !testInfo.deleteDocsOnly) { // (default = delete entire source) deletedSources = true; deletedSource = true; String fileToDelete = new StringBuffer(LOGSTASH_CONFIG).append(testInfo._id.toString()) .append(LOGSTASH_CONFIG_EXTENSION).toString(); boolean deleted = false; try { deleted = new File(fileToDelete).delete(); } catch (Exception e) { } //DEBUG //System.out.println("DELETED CONF FILE" + fileToDelete + " ? " + deleted); _logger.info("delete conf_file=" + fileToDelete + " success=" + deleted); } //TESTED (docs-only + source deletion) // If _not_ deleting the source, then do delete the sincedb file // (else let it get cleaned up separately - minimizes race conditions where the source starts ingesting again) String fileToDelete = new StringBuffer(LOGSTASH_WD).append(".sincedb_") .append(testInfo._id.toString()).toString(); if (!deletedSource) { boolean deleted = false; try { deleted = new File(fileToDelete).delete(); deletedSinceDbs |= deleted; } catch (Exception e) { } //DEBUG //System.out.println("DELETED SINCEDB" + fileToDelete + " ? " + deletedSinceDb); _logger.info("primary delete sincedb_file=" + fileToDelete + " success=" + deleted); } else { deleteAfterRestartQueue.add(fileToDelete); } //TESTED (primary + secondary deletes) } catch (Exception e) { //e.printStackTrace(); } // probably just doesn't exist // Get next element and carry on nextElement = _logHarvesterQ.pop(queueQuery); } //TESTED (end first loop over elements to delete) if (deletedSources || deletedSinceDbs) { // this file actually existed - need to restart the logstash unfortunately _logger.info("Restarting logstash, and sleeping until logstash is restarted"); try { new File(LOGSTASH_RESTART_FILE).createNewFile(); for (int i = 0; i < 12; ++i) { Thread.sleep(10L * 1000L); if (!new File(LOGSTASH_RESTART_FILE).exists()) { Thread.sleep(5L * 1000L); // (extra wait for it to shut down) break; // (early exit) } } } catch (Exception e) { } } //TESTED (from doc deletion and from src deletion) for (String fileToDelete : deleteAfterRestartQueue) { boolean deleted = false; try { deleted = new File(fileToDelete).delete(); } catch (Exception e) { } //DEBUG //System.out.println("DELETED SINCEDB" + fileToDelete + " ? " + deletedSinceDb); _logger.info("secondary delete sincedb_file=" + fileToDelete + " success=" + deleted); } //TESTED (primary and secondary deletion) for (TestLogstashExtractorPojo testInfo : secondaryQueue) { String commIdStr = testInfo.deleteOnlyCommunityId.toString(); // Get all the indexes that might need to be cleansed: ElasticSearchManager indexMgr = ElasticSearchManager.getIndex(DUMMY_INDEX); // Stashed index ArrayList<String> indices = new ArrayList<String>(); String stashedIndex = "recs_" + commIdStr; ClusterStateResponse retVal = indexMgr.getRawClient().admin().cluster().prepareState() .setIndices(stashedIndex).setRoutingTable(false).setNodes(false).setListenerThreaded(false) .get(); if (!retVal.getState().getMetaData().getIndices().isEmpty()) { indices.add(stashedIndex); } // (else doesn't exist...) // Live indexes: String indexPattern = new StringBuffer("recs_t_").append(commIdStr).append("*").toString(); retVal = indexMgr.getRawClient().admin().cluster().prepareState().setIndices(indexPattern) .setRoutingTable(false).setNodes(false).setListenerThreaded(false).get(); for (IndexMetaData indexMetadata : retVal.getState().getMetaData()) { //DEBUG //System.out.println("INDEX=" + indexMetadata.index()); indices.add(indexMetadata.index()); } deleteSourceKeyRecords(indexMgr, indices.toArray(new String[0]), testInfo.sourceKey); _logger.info("Deleted key=" + testInfo.sourceKey + " from indexes=" + ArrayUtils.toString(indices.toArray())); // Now I've deleted, go and distribute the deletion messages to the slaves if ((null != testInfo.distributed) && testInfo.distributed) { // Copy into the slaves' queue DBCursor dbc = DbManager.getIngest().getLogHarvesterSlaves().find(); while (dbc.hasNext()) { BasicDBObject slave = (BasicDBObject) dbc.next(); testInfo.forSlave = slave.getString("_id"); _logHarvesterQ.push(testInfo.toDb()); testInfo.forSlave = null; //DEBUG //System.out.println("DISTRIBUTING DELETION MESSAGE TO " + slave.toString()); _logger.info("distributing deletion message to host=" + slave.toString()); } } //TESTED (by hand) } //(end loop over secondary queue, ie to actually delete the indexes) }