package com.ese.ils.beta.beans; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.URLEncoder; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.ViewScoped; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; import javax.faces.event.ValueChangeEvent; import javax.faces.model.SelectItem; import org.icefaces.apache.commons.io.IOUtils; import com.ese.ils.beta.model.Module; import com.ese.ils.beta.service.ModuleLocalServiceUtil; import com.ese.ils.beta.util.SendEmail; import com.liferay.counter.service.CounterLocalServiceUtil; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.repository.model.FileEntry; import com.liferay.portal.kernel.repository.model.Folder; import com.liferay.portal.model.Group; import com.liferay.portal.service.GroupLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.util.PortalUtil; import com.liferay.portlet.documentlibrary.NoSuchFolderException; import com.liferay.portlet.documentlibrary.model.DLFileEntry; import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil; import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil; import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil; /** * Enthaelt die Implementierung der Gruppenarbeitsfunktionen (Upload von Content, usw.) * Zugriff auf GroupNavigationBean und NavigationBackingBean zur Steuerung der Navigation moeglich. * @author eduard walter * */ @ManagedBean @ViewScoped public class GroupBackingBean implements Serializable { private static final long serialVersionUID = -1934788444798963581L; @ManagedProperty(value = "#{groupNavigationBean}") private transient GroupNavigationBean groupNavigationBean; @ManagedProperty(value="#{navigationBackingBean}") private transient NavigationBackingBean navigationBackingBean; private String sessionTitle; private String sessionId; private String sessionPassword; private String currentModuleId; private List<SelectItem> moduleItems; private List<SelectItem> contentItems; private String currentContentId; private String contentUrl; private String uploadedBy; private boolean disableContentFunctions = true; private boolean showShareMenu = false; private String customShareEmails; private String contentDescription; Folder groupFolder; /** * Navigationsmethode mit Ziel groupModeSelection.xhtml * @param event */ public void moveToGroupModeSelection(ActionEvent event) { navigationBackingBean.setHomeStatus(false); groupNavigationBean.setGroupCreateSessionStatus(false); groupNavigationBean.setGroupLoginStatus(false); groupNavigationBean.setGroupContentViewerStatus(false); groupNavigationBean.setGroupModeSelectionStatus(true); } /** * ChangeListener, der die aktuelle Content-Auswahl erkennt * * @param vcEvent */ public void contentChangeListener(ValueChangeEvent vcEvent){ fetchContent(Long.valueOf(""+vcEvent.getNewValue())); } /** * Versendet E-Mail mit ausgewaehltem Inhalt an alle Gruppenmitglieder * @param event */ public void mailContentToGroupMembers(ActionEvent event){ try { long[] groupUserIds = UserLocalServiceUtil.getGroupUserIds(Long.valueOf(sessionId)); String groupEmails = ""; for(int i=0;i<groupUserIds.length;i++){ groupEmails = groupEmails+UserLocalServiceUtil.getUser(groupUserIds[i]).getEmailAddress()+","; } DLFileEntry fEntry = DLFileEntryLocalServiceUtil.getFileEntry(Long.valueOf(currentContentId)); File file = inputStreamToFile(DLFileEntryLocalServiceUtil.getFileAsStream(fEntry.getUserId(), fEntry.getFileEntryId(), fEntry.getVersion())); File newFilename = new File(fEntry.getTitle()); file.renameTo(newFilename); System.out.println(groupEmails); SendEmail.sendFilePerMail(newFilename, groupEmails); } catch (NumberFormatException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); } } /** * Versendet E-Mail mit ausgewaehltem Inhalt an angegebene E-Mail(s) * @param event */ public void mailContentToCustomMails(ActionEvent event){ try { DLFileEntry fEntry = DLFileEntryLocalServiceUtil.getFileEntry(Long.valueOf(currentContentId)); File file = inputStreamToFile(DLFileEntryLocalServiceUtil.getFileAsStream(fEntry.getUserId(), fEntry.getFileEntryId(), fEntry.getVersion())); File newFilename = new File(fEntry.getTitle()); file.renameTo(newFilename); System.out.println(customShareEmails); SendEmail.sendFilePerMail(newFilename, customShareEmails); } catch (NumberFormatException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); } } /** * Hilfsmethode, wandelt einen InputStream in ein File-Objekt um, * damit dieses versendet werden kann * @param inputStream * @return file */ public File inputStreamToFile(InputStream inputStream){ File file = new File("tmp.tmp"); OutputStream outputStream; try { outputStream = new FileOutputStream(file); IOUtils.copy(inputStream, outputStream); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return file; } /** * Navigationsmethode mit Ziel groupCreateSession.xhtml * Fuer die Modulauswahl werden hier zudem verfuegbare Module geladen. * @param event */ public void moveToGroupCreateSession(ActionEvent event){ try { moduleItems = new ArrayList<SelectItem>(); for (Module module : ModuleLocalServiceUtil.fetchItemsByUser(getCurrentUserId())) { moduleItems.add(new SelectItem(module.getModuleId(),module.getModuleTitle())); } } catch (Exception e) { e.printStackTrace(); } groupNavigationBean.setGroupModeSelectionStatus(false); groupNavigationBean.setGroupSessionCreatedStatus(false); groupNavigationBean.setGroupCreateSessionStatus(true); } /** * Navigationsmethode mit Ziel groupSessionCreated.xhtml * Per createSession() wird eine Gruppe angelegt * @param event */ public void moveToGroupSessionCreated(ActionEvent event){ createSession(); groupNavigationBean.setGroupCreateSessionStatus(false); groupNavigationBean.setGroupSessionCreatedStatus(true); } /** * Benutzt die Liferay-Gruppenfunktionen, um eine Session anzulegen */ public void createSession() { Random rand = new Random(); int n = rand.nextInt(99999); long gpId; try { gpId = CounterLocalServiceUtil.increment(Group.class.getName()); Group userGrp = GroupLocalServiceUtil.createGroup(gpId); userGrp.setClassNameId(PortalUtil.getClassNameId(Group.class.getName())); userGrp.setClassPK(Long.valueOf(n)); userGrp.setCompanyId(Long.valueOf(n+2)); userGrp.setName(sessionTitle); userGrp.setCreatorUserId(getCurrentUserId()); userGrp.setActive(true); userGrp.setDescription(currentModuleId); Group sessionGroup = GroupLocalServiceUtil.addGroup(userGrp); sessionId = ""+sessionGroup.getGroupId(); long tmp = 1337+sessionGroup.getGroupId(); sessionPassword = ""+tmp; System.out.println("Gruppe erstellt!!!"); } catch (SystemException e) { e.printStackTrace(); } } /** * User, die einer Gruppe beitreten werden dieser zugeordnet. * Hierfuer muss geprueft werden, ob der aktuelle user bereits in dieser gruppe ist. * @param userId * @param sessionId */ public void joinSession(long userId, long sessionId) { //user nicht zwei mal adden! contentUrl=""; try { long[] groupUserIds = UserLocalServiceUtil.getGroupUserIds(sessionId); outerloop: if(groupUserIds.length!=0){ for(int i=0;i<groupUserIds.length;i++){ if(groupUserIds[i]==userId){ break outerloop; } } long[] tmpGroupIds = new long[groupUserIds.length+1]; System.arraycopy(groupUserIds, 0, tmpGroupIds, 0, groupUserIds.length); tmpGroupIds[tmpGroupIds.length-1]=getCurrentUserId(); groupUserIds=tmpGroupIds; } else { groupUserIds = new long[]{userId}; } UserLocalServiceUtil.addGroupUsers(sessionId, groupUserIds); } catch (SystemException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); } } /** * Authentifizierungsservice fuer Gruppenlogin * @param event */ public void sessionLogin(ActionEvent event){ System.out.println("sessionLogin..."); long tmp = Long.valueOf(sessionId)+1337; //exmpl: 901 -> 2238 try{ GroupLocalServiceUtil.getGroup(Long.valueOf(sessionId)); if(sessionPassword.equals(""+tmp)){ moveToGroupContentSelection(event); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); } } /** * Navigationsmethode mit Ziel groupLogin.xhtml * @param event */ public void moveToGroupLogin(ActionEvent event) { groupNavigationBean.setGroupModeSelectionStatus(false); groupNavigationBean.setGroupLoginStatus(true); groupNavigationBean.setGroupContentSelectionStatus(false); } /** * Navigationsmethode mit Ziel groupContentSelection.xhtml * fuegt einen user einer gruppe zu und laedt gruppeninhalte vor * @param event */ public void moveToGroupContentSelection(ActionEvent event) { System.out.println("joining Session... with SessionId: " +sessionId); joinSession(getCurrentUserId(),Long.valueOf(sessionId)); System.out.println("fetching Content Items..."); fetchContentItems(); System.out.println("Setting View Status..."); groupNavigationBean.setGroupLoginStatus(false); groupNavigationBean.setGroupContentViewerStatus(false); groupNavigationBean.setGroupSessionCreatedStatus(false); groupNavigationBean.setGroupContentSelectionStatus(true); } /** * laedt gruppeninhalte in eine Liste vom Typ SelectItem, zur Darstellung in * Select-Box, hier wird die Liferay-Document and Media Library verwendet */ public void fetchContentItems() { //group folder im repository-root muss vorhanden sein try { contentItems = new ArrayList<SelectItem>(); Folder groupParentFolder = DLAppServiceUtil.getFolder(25509, 0, "group"); groupFolder = DLAppServiceUtil.getFolder(25509, groupParentFolder.getFolderId(), "" + sessionId); for (FileEntry fileEntry : DLAppLocalServiceUtil.getFileEntries(25509, groupFolder.getFolderId())) { contentItems.add(new SelectItem(fileEntry.getFileEntryId(),fileEntry.getTitle())); } } catch (SystemException e) { e.printStackTrace(); } catch (NoSuchFolderException e){ System.out.println("nf "+e.getMessage()); } catch (PortalException e) { System.out.println(e.getMessage()); } } /** * Navigationsmethode mit Ziel groupContentViewer.xhtml * @param event */ public void moveToGroupContentViewer(ActionEvent event) { fetchContent(Long.valueOf(currentContentId)); groupNavigationBean.setGroupContentSelectionStatus(false); groupNavigationBean.setGroupContentViewerStatus(true); } /** * Laedt einen Gruppeninhalt, damit dieser im Gesamten dargestellt werden kann * @param contentId */ public void fetchContent(long contentId) { //load & process single content like slide String prefix; try { prefix = PortalUtil.getPortalURL(InetAddress.getLocalHost().getHostAddress(), PortalUtil.getPortalPort(), false); DLFileEntry entry = DLFileEntryLocalServiceUtil.getDLFileEntry(contentId); setUploadedBy(entry.getUserName()); setContentDescription(entry.getDescription()); contentUrl = (prefix+"/documents/" + entry.getGroupId() + "/" + entry.getFolderId() + "/" + URLEncoder.encode(entry.getTitle(), "utf-8")); System.out.println(contentUrl); } catch (UnknownHostException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * Versendet aktuellen Content per Mail an aktuellen User * @param event */ public void mailContent(ActionEvent event){ try { DLFileEntry fEntry = DLFileEntryLocalServiceUtil.getFileEntry(Long.valueOf(currentContentId)); File file = inputStreamToFile(DLFileEntryLocalServiceUtil.getFileAsStream(fEntry.getUserId(), fEntry.getFileEntryId(), fEntry.getVersion())); File newFilename = new File(fEntry.getTitle()); file.renameTo(newFilename); SendEmail.sendFilePerMail(newFilename,UserLocalServiceUtil.getUser(getCurrentUserId()).getEmailAddress()); } catch (NumberFormatException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } } /** * Schaltet das Menue zwischen normalen und sharing-optionen um * @param event */ public void shareSwitch(ActionEvent event){ groupNavigationBean.setGroupContentUploadStatus(false); setShowShareMenu(!isShowShareMenu()); } /** * Blendet das Hochlade-Menue ein * @param event */ public void switchGroupContentUpload(ActionEvent event){ groupNavigationBean.setGroupContentUploadStatus(!groupNavigationBean.isGroupContentUploadStatus()); } /** * Hilfsmethode zur Ermittlung der aktuellen UserId * @return */ private long getCurrentUserId() { System.out.println("Getting current UserId..."); FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext externalContext = fc.getExternalContext(); long id = Long.parseLong(externalContext.getUserPrincipal().getName()); System.out.println("Current user id is: " + id); return id; } public GroupNavigationBean getGroupNavigationBean() { return groupNavigationBean; } public void setGroupNavigationBean(GroupNavigationBean groupNavigationBean) { this.groupNavigationBean = groupNavigationBean; } public NavigationBackingBean getNavigationBackingBean() { return navigationBackingBean; } public void setNavigationBackingBean(NavigationBackingBean navigationBackingBean) { this.navigationBackingBean = navigationBackingBean; } public String getSessionTitle() { return sessionTitle; } public void setSessionTitle(String sessionTitle) { this.sessionTitle = sessionTitle; } public String getCurrentModuleId() { return currentModuleId; } public void setCurrentModuleId(String currentModuleId) { this.currentModuleId = currentModuleId; } public List<SelectItem> getModuleItems() { return moduleItems; } public void setModuleItems(List<SelectItem> moduleItems) { this.moduleItems = moduleItems; } public String getSessionId() { return sessionId; } public void setSessionId(String sessionId) { this.sessionId = sessionId; } public String getSessionPassword() { return sessionPassword; } public void setSessionPassword(String sessionPassword) { this.sessionPassword = sessionPassword; } public List<SelectItem> getContentItems() { return contentItems; } public void setContentItems(List<SelectItem> contentItems) { this.contentItems = contentItems; } public String getCurrentContentId() { return currentContentId; } public void setCurrentContentId(String currentContentId) { this.currentContentId = currentContentId; } public String getContentUrl() { return contentUrl; } public void setContentUrl(String contentUrl) { this.contentUrl = contentUrl; } public boolean isDisableContentFunctions() { System.out.println("content "+currentContentId); if(currentContentId==null||currentContentId==""){ return true; } else { fetchContent(Long.valueOf(currentContentId)); return false; } } public void setDisableContentFunctions(boolean disableContentFunctions) { this.disableContentFunctions = disableContentFunctions; } public String getUploadedBy() { return uploadedBy; } public void setUploadedBy(String uploadedBy) { this.uploadedBy = uploadedBy; } public boolean isShowShareMenu() { return showShareMenu; } public void setShowShareMenu(boolean showShareMenu) { this.showShareMenu = showShareMenu; } public String getCustomShareEmails() { return customShareEmails; } public void setCustomShareEmails(String customShareEmails) { this.customShareEmails = customShareEmails; } public String getContentDescription() { return contentDescription; } public void setContentDescription(String contentDescription) { this.contentDescription = contentDescription; } }