Skip to content

Instantly share code, notes, and snippets.

@floverfelt
Created May 13, 2021 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floverfelt/6b4c11441868a5e3ff2010251029fffe to your computer and use it in GitHub Desktop.
Save floverfelt/6b4c11441868a5e3ff2010251029fffe to your computer and use it in GitHub Desktop.
package fqcn;
import com.microstrategy.web.app.addons.AbstractAppAddOn;
import com.microstrategy.web.app.beans.PageComponent;
import com.microstrategy.web.beans.RWBean;
import com.microstrategy.web.objects.EnumWebPromptType;
import com.microstrategy.web.objects.WebConstantPrompt;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.WebPrompt;
import com.microstrategy.web.objects.WebPrompts;
import com.microstrategy.web.objects.admin.users.WebUser;
import com.microstrategy.web.objects.admin.users.WebUserEntity;
import com.microstrategy.web.objects.rw.RWInstance;
import java.util.Enumeration;
public class AnswerDocumentPromptAddon extends AbstractAppAddOn {
@Override
public String getAddOnDescription() {
return null;
}
@Override
public void preCollectData(PageComponent page) {
RWBean rwBean = (RWBean) page.getChildByClass(RWBean.class);
try {
// The guid of the document, if we're not on the doc, skip the addon
if (!"<guid-here>".equals(rwBean.getObjectID())) {
super.preCollectData(page);
}
RWInstance rwInstance = rwBean.getRWInstance();
WebPrompts webPrompts = rwInstance.getPrompts();
WebIServerSession webIServerSession = page.getWebIServerSession();
WebUser user = (WebUser) webIServerSession.getUserInfo(true);
for (int i = 0; i < webPrompts.size(); i++) {
WebPrompt webPrompt = webPrompts.get(i);
if (webPrompt.getPromptType() == EnumWebPromptType.WebPromptTypeConstant) {
WebConstantPrompt webConstantPrompt = (WebConstantPrompt) webPrompt;
// The prompt id we created and put into the XQuery report
if (webConstantPrompt.getID().equals("<guid>")) {
webConstantPrompt.setAnswer(getUsersUserGroupsAsCsv(user));
}
// Programmatically answer the prompt
webConstantPrompt.answerPrompt();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Helper method to get the user groups as a CSV String value.
private String getUsersUserGroupsAsCsv(WebUser user) throws WebObjectsException {
String answer = null;
user.populate();
Enumeration<WebUserEntity> parents = user.getParents().elements();
while (parents.hasMoreElements()) {
WebUserEntity parentGroup = parents.nextElement();
parentGroup.populate();
if (answer == null) {
answer = parentGroup.getFullName();
} else {
answer = String.join(",", answer, parentGroup.getFullName());
}
}
return answer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment