Search in sources :

Example 11 with UploadPortletRequest

use of com.liferay.portal.kernel.upload.UploadPortletRequest in project liferay-ide by liferay.

the class JukeboxPortlet method addAlbum.

public void addAlbum(ActionRequest request, ActionResponse response) throws Exception {
    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);
    long artistId = ParamUtil.getLong(uploadPortletRequest, "artistId");
    String name = ParamUtil.getString(uploadPortletRequest, "name");
    int year = ParamUtil.getInteger(uploadPortletRequest, "year");
    InputStream inputStream = uploadPortletRequest.getFileAsStream("file");
    ServiceContext serviceContext = ServiceContextFactory.getInstance(Album.class.getName(), uploadPortletRequest);
    try {
        AlbumServiceUtil.addAlbum(artistId, name, year, inputStream, serviceContext);
        SessionMessages.add(request, "albumAdded");
        String redirect = ParamUtil.getString(uploadPortletRequest, "redirect");
        response.sendRedirect(redirect);
    } catch (Exception e) {
        SessionErrors.add(request, e.getClass().getName());
        if (e instanceof AlbumNameException || e instanceof PrincipalException) {
            response.setRenderParameter("jspPage", "/html/albums/edit_album.jsp");
        } else {
            response.setRenderParameter("jspPage", "/html/error.jsp");
        }
    }
}
Also used : InputStream(java.io.InputStream) ServiceContext(com.liferay.portal.service.ServiceContext) PrincipalException(com.liferay.portal.security.auth.PrincipalException) Album(org.liferay.jukebox.model.Album) UploadPortletRequest(com.liferay.portal.kernel.upload.UploadPortletRequest) AlbumNameException(org.liferay.jukebox.AlbumNameException) SongNameException(org.liferay.jukebox.SongNameException) PrincipalException(com.liferay.portal.security.auth.PrincipalException) DuplicatedSongException(org.liferay.jukebox.DuplicatedSongException) AlbumNameException(org.liferay.jukebox.AlbumNameException) ArtistNameException(org.liferay.jukebox.ArtistNameException)

Example 12 with UploadPortletRequest

use of com.liferay.portal.kernel.upload.UploadPortletRequest in project liferay-ide by liferay.

the class MeetupsPortlet method updateMeetupsEntry.

public void updateMeetupsEntry(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);
    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
    PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();
    if (!permissionChecker.isCompanyAdmin()) {
        return;
    }
    long meetupsEntryId = ParamUtil.getLong(uploadPortletRequest, "meetupsEntryId");
    String title = ParamUtil.getString(uploadPortletRequest, "title");
    String description = ParamUtil.getString(uploadPortletRequest, "description");
    int startDateMonth = ParamUtil.getInteger(uploadPortletRequest, "startDateMonth");
    int startDateDay = ParamUtil.getInteger(uploadPortletRequest, "startDateDay");
    int startDateYear = ParamUtil.getInteger(uploadPortletRequest, "startDateYear");
    int startDateHour = ParamUtil.getInteger(uploadPortletRequest, "startDateHour");
    int startDateMinute = ParamUtil.getInteger(uploadPortletRequest, "startDateMinute");
    int startDateAmPm = ParamUtil.getInteger(uploadPortletRequest, "startDateAmPm");
    if (startDateAmPm == Calendar.PM) {
        startDateHour += 12;
    }
    int endDateMonth = ParamUtil.getInteger(uploadPortletRequest, "endDateMonth");
    int endDateDay = ParamUtil.getInteger(uploadPortletRequest, "endDateDay");
    int endDateYear = ParamUtil.getInteger(uploadPortletRequest, "endDateYear");
    int endDateHour = ParamUtil.getInteger(uploadPortletRequest, "endDateHour");
    int endDateMinute = ParamUtil.getInteger(uploadPortletRequest, "endDateMinute");
    int endDateAmPm = ParamUtil.getInteger(uploadPortletRequest, "endDateAmPm");
    if (endDateAmPm == Calendar.PM) {
        endDateHour += 12;
    }
    int totalAttendees = ParamUtil.getInteger(uploadPortletRequest, "totalAttendees");
    int maxAttendees = ParamUtil.getInteger(uploadPortletRequest, "maxAttendees");
    double price = ParamUtil.getDouble(uploadPortletRequest, "price");
    File file = uploadPortletRequest.getFile("fileName");
    byte[] bytes = FileUtil.getBytes(file);
    if (meetupsEntryId <= 0) {
        MeetupsEntryLocalServiceUtil.addMeetupsEntry(themeDisplay.getUserId(), title, description, startDateMonth, startDateDay, startDateYear, startDateHour, startDateMinute, endDateMonth, endDateDay, endDateYear, endDateHour, endDateMinute, totalAttendees, maxAttendees, price, bytes);
    } else {
        MeetupsEntryLocalServiceUtil.updateMeetupsEntry(themeDisplay.getUserId(), meetupsEntryId, title, description, startDateMonth, startDateDay, startDateYear, startDateHour, startDateMinute, endDateMonth, endDateDay, endDateYear, endDateHour, endDateMinute, totalAttendees, maxAttendees, price, bytes);
    }
}
Also used : PermissionChecker(com.liferay.portal.security.permission.PermissionChecker) UploadPortletRequest(com.liferay.portal.kernel.upload.UploadPortletRequest) ThemeDisplay(com.liferay.portal.theme.ThemeDisplay) File(java.io.File)

Example 13 with UploadPortletRequest

use of com.liferay.portal.kernel.upload.UploadPortletRequest 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;
        }
    }
}
Also used : User(com.liferay.portal.kernel.model.User) JSONArray(com.liferay.portal.kernel.json.JSONArray) JSONException(com.liferay.portal.kernel.json.JSONException) IOException(java.io.IOException) UploadPortletRequest(com.liferay.portal.kernel.upload.UploadPortletRequest) Role(com.liferay.portal.kernel.model.Role) JSONObject(com.liferay.portal.kernel.json.JSONObject) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) PortalException(com.liferay.portal.kernel.exception.PortalException) File(java.io.File)

Example 14 with UploadPortletRequest

use of com.liferay.portal.kernel.upload.UploadPortletRequest 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);
        }
    }
}
Also used : User(com.liferay.portal.kernel.model.User) JSONArray(com.liferay.portal.kernel.json.JSONArray) JSONException(com.liferay.portal.kernel.json.JSONException) IOException(java.io.IOException) UploadPortletRequest(com.liferay.portal.kernel.upload.UploadPortletRequest) LinkedList(java.util.LinkedList) Role(com.liferay.portal.kernel.model.Role) JSONObject(com.liferay.portal.kernel.json.JSONObject) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) PortalException(com.liferay.portal.kernel.exception.PortalException) File(java.io.File)

Example 15 with UploadPortletRequest

use of com.liferay.portal.kernel.upload.UploadPortletRequest 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;
        }
    }
}
Also used : User(com.liferay.portal.kernel.model.User) Organization(com.liferay.portal.kernel.model.Organization) JSONArray(com.liferay.portal.kernel.json.JSONArray) JSONException(com.liferay.portal.kernel.json.JSONException) IOException(java.io.IOException) UploadPortletRequest(com.liferay.portal.kernel.upload.UploadPortletRequest) UserGroup(com.liferay.portal.kernel.model.UserGroup) Role(com.liferay.portal.kernel.model.Role) JSONObject(com.liferay.portal.kernel.json.JSONObject) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) PortalException(com.liferay.portal.kernel.exception.PortalException) File(java.io.File)

Aggregations

UploadPortletRequest (com.liferay.portal.kernel.upload.UploadPortletRequest)15 InputStream (java.io.InputStream)9 ServiceContext (com.liferay.portal.service.ServiceContext)8 PrincipalException (com.liferay.portal.security.auth.PrincipalException)6 AlbumNameException (org.liferay.jukebox.AlbumNameException)6 ArtistNameException (org.liferay.jukebox.ArtistNameException)6 DuplicatedSongException (org.liferay.jukebox.DuplicatedSongException)6 SongNameException (org.liferay.jukebox.SongNameException)6 File (java.io.File)4 PortalException (com.liferay.portal.kernel.exception.PortalException)3 JSONArray (com.liferay.portal.kernel.json.JSONArray)3 JSONException (com.liferay.portal.kernel.json.JSONException)3 JSONObject (com.liferay.portal.kernel.json.JSONObject)3 Role (com.liferay.portal.kernel.model.Role)3 User (com.liferay.portal.kernel.model.User)3 ThemeDisplay (com.liferay.portal.theme.ThemeDisplay)3 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 Album (org.liferay.jukebox.model.Album)2