public ResponsePojo installModule(String moduleJson, String userIdStr)
{
ResponsePojo rp = new ResponsePojo();
try
{
UIModulePojo module = ApiManager.mapFromApi(moduleJson, UIModulePojo.class, new UIModulePojoApiMap(null));
// (allow all communities submitted by the user)
module.setSwf(""); // (no longer needed)
module.setApproved(true); // (no mechanism for admin authentication / or of communities)
// Check it's all valid (id can be null
if ((null == module.getDescription())||(null == module.getImageurl())
||(null == module.getUrl())||(null == module.getTitle())||(null == module.getVersion()))
{
rp.setResponse(new ResponseObject("Install Module",false,"Missing one of description, image url, url, title, or version"));
return rp;
}//TESTED
else {
boolean bAdmin = RESTTools.adminLookup(userIdStr);
if (!bAdmin) { // need to be owner or moderator to add to a community
if (null == module.getCommunityIds() || module.getCommunityIds().isEmpty()) {
module.addToCommunityIds(new ObjectId(userIdStr)); // (ie adds to personal community)
}
else {
HashSet<ObjectId> newSet = new HashSet<ObjectId>();
for (ObjectId communityId: module.getCommunityIds()) {
if (!SocialUtils.isOwnerOrModerator(communityId.toString(), userIdStr)) {
rp.setResponse(new ResponseObject("Install Module",false,"Don't have permission to update one or more communities: " + communityId.toString()));
return rp;
}
newSet.add(communityId);
}
if (newSet.isEmpty()) {
newSet.add(new ObjectId(userIdStr)); // (ie adds to personal community)
}
module.setCommunityIds(newSet);
}
}//TESTED
// Get username from profile id:
AuthenticationPojo userQuery = new AuthenticationPojo();
userQuery.setProfileId(new ObjectId(userIdStr));
BasicDBObject userDbo = (BasicDBObject) DbManager.getSocial().getAuthentication().findOne(userQuery.toDb());
String userName = userIdStr;
if (null != userDbo) {
AuthenticationPojo user = AuthenticationPojo.fromDb(userDbo, AuthenticationPojo.class);
userName = user.getUsername();
}//TESTED
if (!bAdmin || (null == module.getAuthor())) {
module.setAuthor(userName);
// (if module name set and I'm admin then allow it)
}
if (null == module.get_id()) { // Either new, or no id specified
// Check if it exists (uniquely defined by url + owner)
UIModulePojo queryModule = new UIModulePojo();
queryModule.setUrl(module.getUrl());
queryModule.setAuthor(userName);
BasicDBObject oldModuleDbo = (BasicDBObject) DbManager.getSocial().getUIModules().findOne(queryModule.toDb());
if (null == oldModuleDbo) { // New module
module.set_id(new ObjectId());
module.setCreated(new Date());
//DEBUG
//System.out.println("New module: " + module.getUrl());
}//TESTED
else { // Overwrite
UIModulePojo oldModule = UIModulePojo.fromDb(oldModuleDbo, UIModulePojo.class);
module.set_id(oldModule.get_id());
//DEBUG
//System.out.println("Overwrite module: " + module.get_id());
}//TESTED
module.setModified(new Date());
}//TESTED
else { // Check if it already exists, owner must match if so
UIModulePojo moduleQuery = new UIModulePojo();
moduleQuery.set_id(module.get_id());
BasicDBObject oldModuleDbo = (BasicDBObject) DbManager.getSocial().getUIModules().findOne(moduleQuery.toDb());
if (null == oldModuleDbo) {
// Fine, carry on
//DEBUG
//System.out.println("Id specified for new module: " + module.get_id());
}//TESTED
else {
UIModulePojo oldModule = UIModulePojo.fromDb(oldModuleDbo, UIModulePojo.class);
if ((null != oldModule.getAuthor()) && (!userName.equals(oldModule.getAuthor())))
{ // Owner doesn't match
if (!bAdmin) {
rp.setResponse(new ResponseObject("Install Module",false,"Permissions error: must be either owner or root"));
return rp;
}
}//TESTED
}
//Set modified
module.setModified(new Date());
}
}
// Insert or update:
DbManager.getSocial().getUIModules().save(module.toDb());
UIModulePojo returnVal = new UIModulePojo();
returnVal.set_id(module.get_id());
returnVal.setApproved(true);
rp.setResponse(new ResponseObject("Install Module",true,"module installed/updated successfully"));
rp.setData(returnVal, new UIModulePojoApiMap(null));
}
catch (Exception ex) {
//ex.printStackTrace();