//TODO (): be able to specify not returning content?
public ResponsePojo searchShares(String personIdStr, String searchby, String idStrList, String sharetypes, String skip, String limit, boolean ignoreAdmin, boolean returnContent, boolean searchParent)
{
ResponsePojo rp = new ResponsePojo();
///////////////////////////////////////////////////////////////////////////////////////////////
// Sample search queries
// share/search/
// share/search/?type=binary
// share/search/?searchby=person&id=admin_infinite@ikanow.com
// share/search/?searchby=community&id=4d88d0f1f9a624a4b0c8bd71,4d88d0f1f9a624a4b0c8bd72&type=dataset&skip=0&limit=10
///////////////////////////////////////////////////////////////////////////////////////////////
// Create Query Object
BasicDBObject query = new BasicDBObject();
HashSet<ObjectId> memberOf = SocialUtils.getUserCommunities(personIdStr);
// (need this to sanitize share communities even if searching explicitly by community)
boolean bAdmin = false;
if (!ignoreAdmin) {
bAdmin = RESTTools.adminLookup(personIdStr);
}
// Community search, supports one or more communities._id values
if ((searchby != null) && (searchby.equalsIgnoreCase("community") || searchby.equalsIgnoreCase("communities")))
{
query = getCommunityQueryObject(query, idStrList, personIdStr, ignoreAdmin, bAdmin, memberOf, searchParent);
//TESTED regex and list versions (single and multiple), no allowed commmunity versions
}
else if ((searchby != null) && searchby.equalsIgnoreCase("person"))
{
if ((ignoreAdmin || !bAdmin) || (null == idStrList)) { // not admin or no ids spec'd
query.put("owner._id", new ObjectId(personIdStr));
}//TESTED
else { // admin and spec'd - can either be an array of ids or an array of email addresses
memberOf = null; // (also returns all the communities in the mapper below)
// List of communities to search on
String[] idStrArray = idStrList.split(",");
List<ObjectId> peopleIds = new ArrayList<ObjectId>();
for (String idStr : idStrArray)
{
try {
ObjectId id = new ObjectId(idStr);
peopleIds.add(id);
}
catch (Exception e) { // Try as people's email addresses
query.put("owner.email", new BasicDBObject("$in", idStrArray));
peopleIds.clear();
break;
}//TESTED
}
if (peopleIds.size() > 0) {
BasicDBObject communities = new BasicDBObject();
communities.append("$in", peopleIds);
query.put("owner._id", communities);
}//TESTED
}
//TESTED: nobody, ids, emails
}
else { // Defaults to all communities to which a user belongs (or everything for admins)
if (ignoreAdmin || !bAdmin) {
if (null != memberOf) {
query.put("communities._id", new BasicDBObject("$in", memberOf));
}
else { // (some error but we'll let this go if the share has no security)
query.put("communities", new BasicDBObject("$exists", false));
}
}
else {
memberOf = null; // (also returns all the communities in the mapper below)
}
}
// Search on share type or types
if (sharetypes != null && sharetypes.length() > 0)
{
if (sharetypes.split(",").length > 1)
{
BasicDBObject types = new BasicDBObject();
String[] typeArray = sharetypes.split(",");
types.append("$in", typeArray);
query.put("type", types);
}
else
{
query.put("type", sharetypes);
}
}
//REMOVING BINARY, if you want to return it you can't do deserialize on it
BasicDBObject removeFields = new BasicDBObject("binaryData",false);
if (!returnContent) {
removeFields.put("share", false);
}
try
{
DBCursor dbc = null;
// Attempt to parse skip and limit into ints if they aren't null
int numToSkip = 0;
int limitToNum = 0;
if (skip != null) try { numToSkip = Integer.parseInt(skip); } catch (Exception e) {}
if (limit != null) try { limitToNum = Integer.parseInt(limit); } catch (Exception e) {}
// Run find query based on whether or not to skip and limit the number of results to return
if (skip != null && limit != null)
{
dbc = (DBCursor)DbManager.getSocial().getShare().find(query,removeFields).skip(numToSkip).limit(limitToNum);
}
else if (skip != null && limit == null)
{
dbc = (DBCursor)DbManager.getSocial().getShare().find(query,removeFields).skip(numToSkip);
}
else if (skip == null && limit != null)
{
dbc = (DBCursor)DbManager.getSocial().getShare().find(query,removeFields).limit(limitToNum);
}
else
{
dbc = (DBCursor)DbManager.getSocial().getShare().find(query,removeFields);
}
List<SharePojo> shares = SharePojo.listFromDb(dbc, SharePojo.listType());
if (!shares.isEmpty()) {
Iterator<SharePojo> shareIt = shares.iterator();
while (shareIt.hasNext()) {
SharePojo share = shareIt.next();
if (null != share.getDocumentLocation()) {
try {
if ((null == share.getType()) || !share.getType().equalsIgnoreCase("binary")) {
// (ignore binary references)
share.setShare(this.getReferenceString(share));
}//TESTED
}
catch (Exception e) { // couldn't access data, just remove data from list
share.setShare("{}");
}
}
}//TESTED
rp.setData(shares, new SharePojoApiMap(memberOf));
rp.setResponse(new ResponseObject("Share", true, "Shares returned successfully"));
}
else {
rp.setResponse(new ResponseObject("Share", true, "No shares matching search critera found."));
}
}
catch (Exception e)
{
logger.error("Exception Message: " + e.getMessage(), e);
rp.setResponse(new ResponseObject("Share", false, "Unable to search for shares: " + e.getMessage()));
}
return rp;
}