Liferay get Organization by using User or get All Organizations in liferay database

This blog is just used to record methods I wrote, but they're useless for my project now. In case I need to use them in the future, I record them in the blog.


//In our MVCPortlet, we can get RenderRequest, when we get RenderRequest, we can get user and get user's organization hierarchy.

public static List<String> getTwitterScreenNames(RenderRequest renderRequest) {

List<String> twitterScreenNames = new ArrayList<String>();
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest
.getAttribute(WebKeys.THEME_DISPLAY);
long userId = themeDisplay.getUserId();
try {
List<Organization> orgs = OrganizationLocalServiceUtil
.getUserOrganizations(userId);
for (Organization org : orgs) {
ExpandoBridge bridge = org.getExpandoBridge();
String screenName = (String) ExpandoValueLocalServiceUtil
.getData(bridge.getClassName(),
ExpandoTableConstants.DEFAULT_TABLE_NAME,
"twitter", org.getPrimaryKey());
twitterScreenNames.add(screenName);
while (org.getParentOrganizationId() != 0) {
org = org.getParentOrganization();
bridge = org.getExpandoBridge();
screenName = (String) ExpandoValueLocalServiceUtil.getData(
bridge.getClassName(),
ExpandoTableConstants.DEFAULT_TABLE_NAME,
"twitter", org.getPrimaryKey());
twitterScreenNames.add(screenName);
}
}
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
return twitterScreenNames;

}


//This method is to get all organization in liferay database.

public static List<String> getAllTwitterScreenNames()
throws SystemException, PortalException {
List<String> twitterScreenNames = new ArrayList<String>();
List<Organization> orgs = OrganizationLocalServiceUtil
.getOrganizations(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
for (Organization org : orgs) {
ExpandoBridge bridge = org.getExpandoBridge();
if (bridge.hasAttribute("twitter")) {
String screenName = (String) ExpandoValueLocalServiceUtil
.getData(bridge.getClassName(),
ExpandoTableConstants.DEFAULT_TABLE_NAME,
"twitter", org.getPrimaryKey());
twitterScreenNames.add(screenName);
while (org.getParentOrganizationId() != 0) {
org = org.getParentOrganization();
bridge = org.getExpandoBridge();
screenName = (String) ExpandoValueLocalServiceUtil.getData(
bridge.getClassName(),
ExpandoTableConstants.DEFAULT_TABLE_NAME,
"twitter", org.getPrimaryKey());
twitterScreenNames.add(screenName);
}
}
}
return twitterScreenNames;
}

你可能感兴趣的:(Liferay get Organization by using User or get All Organizations in liferay database)