use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.
the class ArtistStagedModelDataHandler method doImportStagedModel.
@Override
protected void doImportStagedModel(PortletDataContext portletDataContext, Artist artist) throws Exception {
long userId = portletDataContext.getUserId(artist.getUserUuid());
ServiceContext serviceContext = portletDataContext.createServiceContext(artist);
Artist importedArtist = null;
if (portletDataContext.isDataStrategyMirror()) {
Artist existingArtist = ArtistLocalServiceUtil.fetchArtistByUuidAndGroupId(artist.getUuid(), portletDataContext.getScopeGroupId());
if (existingArtist == null) {
serviceContext.setUuid(artist.getUuid());
importedArtist = ArtistLocalServiceUtil.addArtist(userId, artist.getName(), artist.getBio(), null, serviceContext);
} else {
importedArtist = ArtistLocalServiceUtil.updateArtist(userId, existingArtist.getArtistId(), artist.getName(), artist.getBio(), null, serviceContext);
}
} else {
importedArtist = ArtistLocalServiceUtil.addArtist(userId, artist.getName(), artist.getBio(), null, serviceContext);
}
Element artistElement = portletDataContext.getImportDataStagedModelElement(artist);
List<Element> attachmentElements = portletDataContext.getReferenceDataElements(artistElement, FileEntry.class, PortletDataContext.REFERENCE_TYPE_WEAK);
for (Element attachmentElement : attachmentElements) {
String path = attachmentElement.attributeValue("path");
FileEntry fileEntry = (FileEntry) portletDataContext.getZipEntryAsObject(path);
InputStream inputStream = null;
try {
String binPath = attachmentElement.attributeValue("bin-path");
if (Validator.isNull(binPath) && portletDataContext.isPerformDirectBinaryImport()) {
try {
inputStream = _getContentStream(fileEntry);
} catch (NoSuchFileException nsfe) {
}
} else {
inputStream = portletDataContext.getZipEntryAsInputStream(binPath);
}
if (inputStream == null) {
if (_log.isWarnEnabled()) {
_log.warn("Unable to import attachment for file entry " + fileEntry.getFileEntryId());
}
continue;
}
importedArtist = ArtistLocalServiceUtil.updateArtist(userId, importedArtist.getArtistId(), importedArtist.getName(), importedArtist.getBio(), inputStream, serviceContext);
} finally {
StreamUtil.cleanUp(inputStream);
}
}
portletDataContext.importClassedModel(artist, importedArtist);
}
use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.
the class JukeboxPortlet method deleteSong.
public void deleteSong(ActionRequest request, ActionResponse response) throws Exception {
long songId = ParamUtil.getLong(request, "songId");
boolean moveToTrash = ParamUtil.getBoolean(request, "moveToTrash");
ServiceContext serviceContext = ServiceContextFactory.getInstance(Song.class.getName(), request);
try {
if (moveToTrash) {
Song song = SongServiceUtil.moveSongToTrash(songId);
Map<String, String[]> data = new HashMap<String, String[]>();
data.put("deleteEntryClassName", new String[] { Song.class.getName() });
data.put("deleteEntryTitle", new String[] { TrashUtil.getOriginalTitle(song.getName()) });
data.put("restoreEntryIds", new String[] { String.valueOf(songId) });
SessionMessages.add(request, PortalUtil.getPortletId(request) + SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
SessionMessages.add(request, PortalUtil.getPortletId(request) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
} else {
SongServiceUtil.deleteSong(songId, serviceContext);
SessionMessages.add(request, "songDeleted");
}
sendRedirect(request, response);
} catch (Exception e) {
SessionErrors.add(request, e.getClass().getName());
response.setRenderParameter("jspPage", "/html/error.jsp");
}
}
use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.
the class JukeboxPortlet method deleteArtist.
public void deleteArtist(ActionRequest request, ActionResponse response) throws Exception {
long artistId = ParamUtil.getLong(request, "artistId");
ServiceContext serviceContext = ServiceContextFactory.getInstance(Artist.class.getName(), request);
try {
ArtistServiceUtil.deleteArtist(artistId, serviceContext);
SessionMessages.add(request, "artistDeleted");
sendRedirect(request, response);
} catch (Exception e) {
SessionErrors.add(request, e.getClass().getName());
response.setRenderParameter("jspPage", "/html/error.jsp");
}
}
use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.
the class JukeboxPortlet method addArtist.
public void addArtist(ActionRequest request, ActionResponse response) throws Exception {
UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);
String name = ParamUtil.getString(uploadPortletRequest, "name");
String bio = ParamUtil.getString(uploadPortletRequest, "bio");
InputStream inputStream = uploadPortletRequest.getFileAsStream("file");
ServiceContext serviceContext = ServiceContextFactory.getInstance(Artist.class.getName(), uploadPortletRequest);
try {
ArtistServiceUtil.addArtist(name, bio, inputStream, serviceContext);
SessionMessages.add(request, "artistAdded");
String redirect = ParamUtil.getString(uploadPortletRequest, "redirect");
response.sendRedirect(redirect);
} catch (Exception e) {
SessionErrors.add(request, e.getClass().getName());
if (e instanceof ArtistNameException || e instanceof PrincipalException) {
response.setRenderParameter("jspPage", "/html/artists/edit_artist.jsp");
} else {
response.setRenderParameter("jspPage", "/html/error.jsp");
}
}
}
use of com.liferay.portal.service.ServiceContext in project liferay-ide by liferay.
the class JukeboxPortlet method addSong.
public void addSong(ActionRequest request, ActionResponse response) throws Exception {
UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);
long albumId = ParamUtil.getLong(uploadPortletRequest, "albumId");
String name = ParamUtil.getString(uploadPortletRequest, "name");
InputStream songInputStream = uploadPortletRequest.getFileAsStream("songFile");
String songFileName = uploadPortletRequest.getFileName("songFile");
InputStream lyricsInputStream = uploadPortletRequest.getFileAsStream("lyricsFile");
String lyricsFileName = uploadPortletRequest.getFileName("lyricsFile");
ServiceContext serviceContext = ServiceContextFactory.getInstance(Song.class.getName(), uploadPortletRequest);
try {
SongServiceUtil.addSong(albumId, name, songFileName, songInputStream, lyricsFileName, lyricsInputStream, serviceContext);
SessionMessages.add(request, "songAdded");
String redirect = ParamUtil.getString(uploadPortletRequest, "redirect");
response.sendRedirect(redirect);
} catch (Exception e) {
SessionErrors.add(request, e.getClass().getName());
if (e instanceof SongNameException || e instanceof DuplicatedSongException || e instanceof PrincipalException) {
response.setRenderParameter("jspPage", "/html/songs/edit_song.jsp");
} else {
response.setRenderParameter("jspPage", "/html/error.jsp");
}
}
}
Aggregations