use of com.liferay.portal.kernel.json.JSONArray in project liferay-docs by liferay.
the class SampleContentPortlet method addUserGroups.
public void addUserGroups(ActionRequest request, ActionResponse response) {
long companyId = PortalUtil.getDefaultCompanyId();
Role adminRole = null;
try {
adminRole = _roleLocalService.getRole(companyId, "Administrator");
} catch (PortalException pe) {
_log.error(pe);
return;
}
List<User> adminUsers = _userLocalService.getRoleUsers(adminRole.getRoleId());
long adminUserId = adminUsers.get(0).getUserId();
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
String fileName = "userGroupsFile";
File file = uploadRequest.getFile(fileName);
StringBuilder out = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
while (br.ready()) {
out.append(br.readLine());
}
br.close();
} catch (IOException fnfe) {
_log.error(fnfe);
return;
}
String jsonString = out.toString();
JSONObject jsonObject = null;
try {
jsonObject = JSONFactoryUtil.createJSONObject(jsonString);
} catch (JSONException je) {
_log.error(je);
return;
}
JSONArray userGroups = jsonObject.getJSONArray("User Groups");
for (int i = 0; i < userGroups.length(); i++) {
String name = userGroups.getJSONObject(i).getString("Name");
String description = userGroups.getJSONObject(i).getString("Description");
try {
_userGroupLocalService.addUserGroup(adminUserId, companyId, name, description, null);
} catch (PortalException pe) {
_log.error(pe);
return;
}
}
}
use of com.liferay.portal.kernel.json.JSONArray in project liferay-docs by liferay.
the class SampleContentPortlet method addOrganizations.
public void addOrganizations(ActionRequest request, ActionResponse response) {
long companyId = PortalUtil.getDefaultCompanyId();
Role adminRole = null;
try {
adminRole = _roleLocalService.getRole(companyId, "Administrator");
} catch (PortalException pe) {
_log.error(pe);
return;
}
List<User> adminUsers = _userLocalService.getRoleUsers(adminRole.getRoleId());
long adminUserId = adminUsers.get(0).getUserId();
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
String fileName = "organizationsFile";
File file = uploadRequest.getFile(fileName);
StringBuilder out = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
while (br.ready()) {
out.append(br.readLine());
}
br.close();
} catch (IOException fnfe) {
_log.error(fnfe);
return;
}
String jsonString = out.toString();
JSONObject jsonObject = null;
try {
jsonObject = JSONFactoryUtil.createJSONObject(jsonString);
} catch (JSONException je) {
_log.error(je);
return;
}
JSONArray orgs = jsonObject.getJSONArray("Organizations");
Queue<JSONOrgWrapper> queue = new LinkedList<JSONOrgWrapper>();
for (int i = 0; i < orgs.length(); i++) {
JSONOrgWrapper jsonOrgWrapper = new JSONOrgWrapper(orgs.getJSONObject(i), StringPool.BLANK);
queue.add(jsonOrgWrapper);
}
while (!queue.isEmpty()) {
JSONOrgWrapper jsonOrgWrapper = queue.remove();
String orgName = jsonOrgWrapper.getJSONObject().getString("Name");
JSONArray subOrgs = jsonOrgWrapper.getJSONObject().getJSONArray("Organizations");
String parentOrgName = jsonOrgWrapper.getParentOrgName();
if (StringPool.BLANK.equals(parentOrgName)) {
try {
_organizationLocalService.addOrganization(adminUserId, 0L, orgName, true);
} catch (PortalException pe) {
_log.error(pe);
return;
}
} else {
long parentOrgId = _organizationLocalService.getOrganizationId(companyId, parentOrgName);
try {
_organizationLocalService.addOrganization(adminUserId, parentOrgId, orgName, true);
} catch (PortalException pe) {
_log.error(pe);
return;
}
}
if (subOrgs == null) {
continue;
}
for (int i = 0; i < subOrgs.length(); i++) {
JSONOrgWrapper jsonSubOrgWrapper = new JSONOrgWrapper(subOrgs.getJSONObject(i), orgName);
queue.add(jsonSubOrgWrapper);
}
}
}
use of com.liferay.portal.kernel.json.JSONArray in project liferay-docs by liferay.
the class SampleContentPortlet method addUsers.
public void addUsers(ActionRequest request, ActionResponse response) {
long companyId = PortalUtil.getDefaultCompanyId();
Role adminRole = null;
try {
adminRole = _roleLocalService.getRole(companyId, "Administrator");
} catch (PortalException pe) {
_log.error(pe);
return;
}
List<User> adminUsers = _userLocalService.getRoleUsers(adminRole.getRoleId());
long adminUserId = adminUsers.get(0).getUserId();
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
String fileName = "usersFile";
File file = uploadRequest.getFile(fileName);
StringBuilder out = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
while (br.ready()) {
out.append(br.readLine());
}
br.close();
} catch (IOException fnfe) {
_log.error(fnfe);
return;
}
String jsonString = out.toString();
JSONObject jsonObject = null;
try {
jsonObject = JSONFactoryUtil.createJSONObject(jsonString);
} catch (JSONException je) {
_log.error(je);
return;
}
JSONArray users = jsonObject.getJSONArray("Users");
for (int i = 0; i < users.length(); i++) {
String screenName = users.getJSONObject(i).getString("Screen Name");
String firstName = users.getJSONObject(i).getString("First Name");
String lastName = users.getJSONObject(i).getString("Last Name");
boolean male = users.getJSONObject(i).getBoolean("Male");
String jobTitle = users.getJSONObject(i).getString("Job Title");
String emailAddress = users.getJSONObject(i).getString("Email Address");
JSONArray userGroups = users.getJSONObject(i).getJSONArray("User Groups");
int userGroupsLength = userGroups.length();
long[] userGroupIds = new long[userGroupsLength];
for (int j = 0; j < userGroupsLength; j++) {
String userGroupName = userGroups.getString(j);
UserGroup userGroup = null;
try {
userGroup = _userGroupLocalService.getUserGroup(companyId, userGroupName);
} catch (PortalException pe) {
_log.error(pe);
return;
}
long userGroupId = userGroup.getUserGroupId();
userGroupIds[j] = userGroupId;
}
JSONArray organizations = users.getJSONObject(i).getJSONArray("Organizations");
int organizationsLength = organizations.length();
long[] organizationIds = new long[organizationsLength];
for (int j = 0; j < organizationsLength; j++) {
String organizationName = organizations.getString(j);
Organization organization = null;
try {
organization = _organizationLocalService.getOrganization(companyId, organizationName);
} catch (PortalException pe) {
_log.error(pe);
return;
}
long organizationId = organization.getOrganizationId();
organizationIds[j] = organizationId;
}
try {
_userLocalService.addUser(adminUserId, companyId, false, "liferay", "liferay", false, screenName, emailAddress, 0L, StringPool.BLANK, LocaleUtil.getDefault(), firstName, StringPool.BLANK, lastName, 0L, 0L, male, Calendar.JANUARY, 1, 1970, jobTitle, new long[0], organizationIds, new long[0], userGroupIds, false, null);
} catch (PortalException pe) {
_log.error(pe);
return;
}
}
}
Aggregations