--- /dev/null
+/**
+ * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.pentila.entSavoie.portlets.userManagement;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.zip.Deflater;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import javax.naming.NamingException;
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.GenericPortlet;
+import javax.portlet.PortletException;
+import javax.portlet.PortletPreferences;
+import javax.portlet.PortletRequestDispatcher;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import javax.portlet.ResourceRequest;
+import javax.portlet.ResourceResponse;
+import javax.servlet.http.HttpServletResponse;
+
+import com.liferay.compat.portal.util.PortalUtil;
+import com.liferay.portal.kernel.dao.orm.QueryUtil;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONException;
+import com.liferay.portal.kernel.json.JSONFactoryUtil;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.kernel.repository.model.FileEntry;
+import com.liferay.portal.kernel.util.ContentTypes;
+import com.liferay.portal.kernel.util.FileUtil;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.liferay.portal.kernel.util.WebKeys;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.User;
+import com.liferay.portal.theme.ThemeDisplay;
+import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
+import com.pentila.entSavoie.ENTDisplayUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+import com.pentila.entSavoie.portlets.userManagement.bdd.util.BDDUtil;
+import com.pentila.entSavoie.portlets.userManagement.ldap.util.LDAPUtil;
+import com.pentila.entSavoie.portlets.userManagement.util.ENTHelper;
+import com.pentila.entSavoie.portlets.userManagement.util.ExportUtil;
+import com.pentila.entSavoie.portlets.userManagement.util.JsonHelper;
+import com.pentila.entSavoie.portlets.userManagement.util.PortletPropsValues;
+import com.pentila.entSavoie.utils.DLAppUtil;
+import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
+import com.pentila.entSavoie.utils.JSPPortletUtil;
+
+/**
+ * <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
+ *
+ * @author Brian Wing Shun Chan
+ *
+ */
+public class JSPPortlet extends GenericPortlet {
+
+ public void init() throws PortletException {
+ editJSP = getInitParameter("edit-jsp");
+ helpJSP = getInitParameter("help-jsp");
+ viewJSP = getInitParameter("view-jsp");
+ }
+
+ public void doDispatch(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ String jspPage = renderRequest.getParameter("jspPage");
+
+ if (jspPage != null) {
+ include(jspPage, renderRequest, renderResponse);
+ }
+ else {
+ super.doDispatch(renderRequest, renderResponse);
+ }
+ }
+
+ public void doEdit(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ if (renderRequest.getPreferences() == null) {
+ super.doEdit(renderRequest, renderResponse);
+ }
+ else {
+ include(editJSP, renderRequest, renderResponse);
+ }
+ }
+
+ public void doHelp(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ include(helpJSP, renderRequest, renderResponse);
+ }
+
+ public void doView(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ include(viewJSP, renderRequest, renderResponse);
+ }
+
+ @Override
+ public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
+ throws PortletException, IOException {
+
+ final String cmd = ParamUtil.getString(resourceRequest, "cmd");
+
+ HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(resourceResponse);
+ httpResponse.setContentType(ContentTypes.APPLICATION_JSON);
+
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", false);
+
+ // Export des utilisateurs
+ if(cmd.equals("exportUser")){
+ try {
+ result = this.exportUser(resourceRequest);
+ }
+ catch (Exception e){
+ _log.error(e);
+ }
+ }
+ else
+ // Obtention des donnees pour la fenetre d'ajout des utilisateurs
+ if(cmd.equals("getAddUserWindowData")){
+ try {
+ result = this.getAddUserWindowData(resourceRequest);
+ }
+ catch (Exception e){
+ _log.error(e);
+ }
+ }
+ else
+ // Ajout d'un nouvel utilisateur
+ if(cmd.equals("addUser")){
+ try {
+ result = this.addUser(resourceRequest);
+ }
+ catch (Exception e){
+ _log.error(e);
+ }
+ }
+ else
+ // Edition d'un utilisateur
+ if(cmd.equals("editUser")){
+ try {
+ result = this.editUser(resourceRequest);
+ }
+ catch (Exception e){
+ _log.error(e);
+ }
+ }
+ else
+ // Suppression d'un utilisateur
+ if(cmd.equals("deleteUser")){
+ try {
+ result = this.deleteUser(resourceRequest);
+ }
+ catch (Exception e){
+ _log.error(e);
+ }
+ }
+ else
+ // Modification du mot de passe d'un utilisateur
+ if(cmd.equals("editPassword")){
+ try {
+ result = this.editPassword(resourceRequest);
+ }
+ catch (Exception e){
+ _log.error(e);
+ }
+ }
+ else
+ // Ajout du fichier a importer
+ if (cmd.equals("addImportFile")) {
+ try {
+ result = this.addImportFile(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+ else
+ // Verification du fichier a importer
+ if (cmd.equals("checkFile")) {
+ try {
+ result = this.checkFile(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+ else
+ // Import du fichier
+ if (cmd.equals("importFile")) {
+ try {
+ result = this.importFile(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+
+ //get the max file upload size
+ if (cmd.equals("getMaxFileUploadSize")) {
+ try {
+ User user = PortalUtil.getUser(resourceRequest);
+ long uploadMaxSize = ENTMainUtilsLocalServiceUtil.getUploadServletRequestMaxSize(user.getCompanyId());
+ result.put("uploadMaxSize", JSPPortletUtil.getSizeInMegaOctet(uploadMaxSize));
+ result.put("success", true);
+ } catch(Exception e){
+ _log.error(e);
+ }
+ }
+
+ httpResponse.getWriter().write(result.toString());
+ }
+
+ public void processAction(
+ ActionRequest actionRequest, ActionResponse actionResponse)
+ throws IOException, PortletException {
+
+ }
+
+ private static String logs = "";
+ private static String logsError = "";
+
+ static SimpleDateFormat frenchDateFormat = new SimpleDateFormat("dd-MM-yyyy");
+
+ // Export des utilisateurs
+ private JSONObject exportUser(ResourceRequest resourceRequest) throws Exception {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);
+ User user = PortalUtil.getUser(resourceRequest);
+
+ final JSONObject exportParams = JSONFactoryUtil.createJSONObject(ParamUtil.getString(resourceRequest, "exportParams"));
+ boolean onlyNeverLogin = ParamUtil.getBoolean(resourceRequest, "onlyNeverLogin");
+
+ JSONObject searchResult = JSONFactoryUtil.createJSONObject();
+
+ JSONArray users = null;
+ byte[] bytes = "".getBytes();
+ String extension = "";
+
+ /*
+ * Export complet
+ */
+ if(exportParams.getString("export").equals("all")){
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ZipOutputStream out = new ZipOutputStream(baos);
+ out.setMethod(ZipOutputStream.DEFLATED);
+ // Niveau de compression des fichiers
+ out.setLevel(Deflater.BEST_COMPRESSION);
+
+ String role = exportParams.getString("type").equals("poste")?"National_2":"";
+
+ Organization etabRattach = (OrganizationFinderServiceUtil.getEtabRatachement(user));
+ List<Organization> userOrgs = etabRattach.getSuborganizations();
+
+ for(Organization org : userOrgs){
+
+ PortletPreferences prefs = org.getPreferences();
+ if (prefs.getValue("migrate", "none").equals("done") ||
+ (exportParams.getString("type").equals("poste") && !org.getName().contains("- Parent"))) {
+ continue;
+ }
+
+ searchResult = LDAPUtil.getUsersAsJSON(
+ user.getCompanyId(),
+ "",
+ "",
+ org.getOrganizationId(),
+ "",
+ "",
+ "",
+ role,
+ true,
+ !onlyNeverLogin,
+ false,
+ true,
+ QueryUtil.ALL_POS,
+ QueryUtil.ALL_POS);
+
+ users = searchResult.getJSONArray("users");
+
+ if(users.length()>0){
+ if(exportParams.getString("type").equals("pdf")){
+ bytes = ExportUtil.exportPDF(users);
+ extension = "pdf";
+ }
+ else if(exportParams.getString("type").equals("csv")){
+ bytes = ExportUtil.exportCSV(users);
+ extension = "csv";
+ }
+ else if(exportParams.getString("type").equals("poste")){
+ bytes = ExportUtil.exportPDFPublipostage(users);
+ extension = "pdf";
+ }
+
+ ZipEntry entry = new ZipEntry(ENTDisplayUtil.formatSCONETName(user, org.getName(), false) + "." + extension);
+ out.putNextEntry(entry);
+ entry.setSize(bytes.length);
+ out.write(bytes);
+ out.closeEntry();
+ }
+ }
+
+ baos.close();
+ bytes = baos.toByteArray();
+
+ extension = "zip";
+ }
+ /*
+ * Export des resultats de recherche
+ */
+ else if(exportParams.getString("export").equals("search")){
+
+ final JSONObject searchParams = JSONFactoryUtil.createJSONObject(ParamUtil.getString(resourceRequest, "searchParams"));
+ String role = exportParams.getString("type").equals("poste")?"National_2":searchParams.getString("role");
+
+ searchResult = LDAPUtil.getUsersAsJSON(
+ user.getCompanyId(),
+ searchParams.getString("etabDN"),
+ searchParams.getString("classDN"),
+ searchParams.getLong("orgId"),
+ searchParams.getString("lastName"),
+ searchParams.getString("firstName"),
+ searchParams.getString("screenName"),
+ role,
+ searchParams.getBoolean("neverLogin"),
+ searchParams.getBoolean("alreadyLogin"),
+ PortletPropsValues.LDAP_USER_GROUPS_INFORM,
+ true,
+ QueryUtil.ALL_POS,
+ QueryUtil.ALL_POS);
+
+ users = searchResult.getJSONArray("users");
+
+ if(users.length()>0){
+ if(exportParams.getString("type").equals("pdf")){
+ bytes = ExportUtil.exportPDF(users);
+ extension = "pdf";
+ }
+ else if(exportParams.getString("type").equals("csv")){
+ bytes = ExportUtil.exportCSV(users);
+ extension = "csv";
+ }
+ else if(exportParams.getString("type").equals("poste")){
+ bytes = ExportUtil.exportPDFPublipostage(users);
+ extension = "pdf";
+ }
+ }
+ }
+ /*
+ * Export des parents et eleves
+ */
+ else if(exportParams.getString("export").equals("diff")){
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ZipOutputStream out = new ZipOutputStream(baos);
+ out.setMethod(ZipOutputStream.DEFLATED);
+ // Niveau de compression des fichiers
+ out.setLevel(Deflater.BEST_COMPRESSION);
+
+ Organization etabRattach = (OrganizationFinderServiceUtil.getEtabRatachement(user));
+ List<Organization> userOrgs = etabRattach.getSuborganizations();
+
+ for(Organization org : userOrgs){
+
+ PortletPreferences prefs = org.getPreferences();
+ if (prefs.getValue("migrate", "none").equals("done") ||
+ org.getName().endsWith("- Parent") ||
+ org.getName().contains("- Enseignants") ||
+ org.getName().contains("- Personnels")) {
+ continue;
+ }
+
+ searchResult = LDAPUtil.getUsersAsJSON(
+ user.getCompanyId(),
+ "",
+ "",
+ 0,
+ "",
+ "",
+ "",
+ "National_1&National_2", // Eleves et Parents
+ true,
+ !onlyNeverLogin,
+ PortletPropsValues.LDAP_USER_GROUPS_INFORM,
+ true,
+ QueryUtil.ALL_POS,
+ QueryUtil.ALL_POS);
+
+ users = searchResult.getJSONArray("users");
+
+ if(users.length()>0){
+ if(exportParams.getString("type").equals("pdf")){
+ bytes = ExportUtil.exportPDF(users);
+ extension = "pdf";
+ }
+ else if(exportParams.getString("type").equals("csv")){
+ bytes = ExportUtil.exportCSV(users);
+ extension = "csv";
+ }
+ else if(exportParams.getString("type").equals("poste")){
+ bytes = ExportUtil.exportPDFPublipostage(users);
+ extension = "pdf";
+ }
+
+ ZipEntry entry = new ZipEntry(ENTDisplayUtil.formatSCONETName(user, org.getName(), false) + "." + extension);
+ out.putNextEntry(entry);
+ entry.setSize(bytes.length);
+ out.write(bytes);
+ out.closeEntry();
+ }
+ }
+
+ baos.close();
+ bytes = baos.toByteArray();
+
+ extension = "zip";
+ }
+
+ String fileName = "Export_Utilisateurs_" + frenchDateFormat.format(new Date()) + "." + extension;
+ String folderName = DLAppUtil.sendToDropbox(user, bytes, fileName);
+ String hrefFile = "<a target='_blank' href='" + themeDisplay.getURLPortal().toString() + "/" + themeDisplay.getPathMain() + "/document_library/get_file?p_l_id=" + themeDisplay.getPlid() + folderName + "'> <br /> <br />" + fileName +"</a>";
+
+ result.put("msg", "Un export au format " + extension + " a été déposé dans votre casier : " + hrefFile);
+
+ return result;
+ }
+
+ // Obtention des donnees pour la fenetre d'ajout des utilisateurs
+ private JSONObject getAddUserWindowData(ResourceRequest resourceRequest) throws NamingException, PortalException, SystemException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ User user = PortalUtil.getUser(resourceRequest);
+
+ boolean loadData = ParamUtil.getBoolean(resourceRequest, "loadData");
+ String userDN = ParamUtil.getString(resourceRequest, "userDN");
+
+ if(loadData){
+ JSONObject rolesData = JsonHelper.createWrapper(ENTHelper.getRoles(user));
+ JSONObject functionsData = JsonHelper.createWrapper(ENTHelper.getFonctions(user));
+ JSONObject disciplinesData = JsonHelper.createWrapper(ENTHelper.getDisciplines(user));
+ JSONObject instrumentsData = JsonHelper.createWrapper(ENTHelper.getInstruments(user));
+
+ result.put("rolesData", rolesData);
+ result.put("functionsData", functionsData);
+ result.put("disciplinesData", disciplinesData);
+ result.put("instrumentsData", instrumentsData);
+ }
+
+ if(!userDN.isEmpty()){
+ result.put("user", JsonHelper.createWrapper(LDAPUtil.getUser(user.getCompanyId(), userDN)).toString());
+ }
+
+ return result;
+ }
+
+ // Ajout d'un nouvel utilisateur
+ private JSONObject addUser(ResourceRequest resourceRequest) throws JSONException, NamingException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ long companyId = PortalUtil.getCompanyId(resourceRequest);
+
+ final JSONObject user = JSONFactoryUtil.createJSONObject(ParamUtil.getString(resourceRequest, "user"));
+ LDAPUtil.addUser(companyId, user);
+
+ return result;
+ }
+
+ // Edition d'un utilisateur
+ private JSONObject editUser(ResourceRequest resourceRequest) throws Exception {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ User currUser = PortalUtil.getUser(resourceRequest);
+ long companyId = PortalUtil.getCompanyId(resourceRequest);
+
+ final JSONObject user = JSONFactoryUtil.createJSONObject(ParamUtil.getString(resourceRequest, "user"));
+ // Si l'utilisateur a ete cree manuellement
+ if(user.getBoolean("handmake")){
+ LDAPUtil.editUser(companyId, currUser, user);
+ }
+ else {
+ if(user.getLong("userId")>0){
+ BDDUtil.updateUserOrganizations(currUser, user.getLong("userId"), user.getJSONArray("organizations"));
+ }
+ }
+
+ return result;
+ }
+
+ // Suppression d'un utilisateur
+ private JSONObject deleteUser(ResourceRequest resourceRequest) throws NamingException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ long companyId = PortalUtil.getCompanyId(resourceRequest);
+
+ String userDN = ParamUtil.getString(resourceRequest, "userDN");
+
+ LDAPUtil.deleteUser(companyId, userDN);
+
+ return result;
+ }
+
+ // Modification du mot de passe d'un utilisateur
+ private JSONObject editPassword(ResourceRequest resourceRequest) throws PortalException, SystemException, NamingException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ long userId = ParamUtil.getLong(resourceRequest, "userId");
+ String password = ParamUtil.getString(resourceRequest, "password");
+ String confirm = ParamUtil.getString(resourceRequest, "confirm");
+
+ String msg = "";
+
+ if(userId>0){
+ msg = BDDUtil._updateUserPassword(userId, password, confirm);
+ }
+ else {
+ long companyId = PortalUtil.getCompanyId(resourceRequest);
+ String userDN = ParamUtil.getString(resourceRequest, "userDN");
+ msg = LDAPUtil.editUserPassword(companyId, userDN, password);
+ }
+
+ if(!msg.isEmpty()){
+ result.put("message", msg);
+ }
+
+ return result;
+ }
+
+ // Ajout du fichier a importer
+ private JSONObject addImportFile(ResourceRequest resourceRequest) throws PortalException, SystemException {
+ return JSPPortletUtil.addFileEntry(resourceRequest);
+ }
+
+ // Verification du fichier a importer
+ private JSONObject checkFile(ResourceRequest resourceRequest) throws NamingException, Exception {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ User user = PortalUtil.getUser(resourceRequest);
+
+ long fileId = ParamUtil.getLong(resourceRequest, "fileId");
+ String importType = ParamUtil.getString(resourceRequest, "importType");
+
+ if(fileId>0){
+ String ou = OrganizationFinderServiceUtil.getEtabLDAPAttribute(OrganizationFinderServiceUtil.getEtabRatachement(user), "ou");
+
+ String commande = "";
+ if(importType.equals("enseignant")){
+ commande = PortletPropsValues.ENT_IMPORT_ENSEIGNANT_SCRIPT_LOGS;
+ }
+ else if(importType.equals("eleve")){
+ commande = PortletPropsValues.ENT_IMPORT_ELEVE_SCRIPT_LOGS;
+ }
+
+ if(!commande.isEmpty()){
+
+ FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileId);
+ File file = FileUtil.createTempFile("xml");
+ FileUtil.write(file, fileEntry.getContentStream());
+
+ String params = " --context_param companyId=" + user.getCompanyId() +
+ " --context_param etabId=" + ou +
+ " --context_param orgRootPortalMembersName=" + PortletPropsValues.ENT_IMPORT_ORG_ROOT_PORTAL_MEMBERS_NAME +
+ " --context_param reportPath=" + PortletPropsValues.ENT_IMPORT_REPORT_PATH +
+ " --context_param file=" + file.getAbsolutePath();
+
+ commande += params;
+
+ System.out.println("Commande : \n" + commande);
+
+ String[] cmd = commande.split(" ");
+ try {
+ final Process p = Runtime.getRuntime().exec(cmd);
+
+ logs = "";
+ new Thread() {
+ public void run() {
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line = "";
+ try {
+ while((line = reader.readLine()) != null) {
+ if(!line.contains("+")){
+ line = line.trim();
+ String[] fields = line.split("\\|");
+ if(fields.length < 2 && !logs.isEmpty()){
+ logs += "</table>";
+ logs += "<br/>";
+ logs += "<table width='100%'>";
+ }
+ else if(fields.length == 2){
+ logs += "<div style='font-weight: bold; font-size: 12px;'>" +
+ " - " + fields[1] + " : " +
+ "</div>" +
+ "<br/>" +
+ "<table width='95%' style='margin: 0 auto;'>";
+ }
+ else {
+ logs += formatLogLine(fields);
+ }
+ }
+ }
+ } finally {
+ reader.close();
+ }
+ } catch(IOException ioe) {
+ _log.error(ioe);
+ }
+ }
+ }.start();
+
+ logsError = "<div>";
+ new Thread() {
+ public void run() {
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
+ String line = "";
+ try {
+ while((line = reader.readLine()) != null) {
+ logsError += line + "<br/>";
+ }
+ } finally {
+ reader.close();
+ }
+ } catch(IOException ioe) {
+ _log.error(ioe);
+ }
+ }
+ }.start();
+ p.waitFor();
+
+ logs += "</table>";
+
+ if(logsError.equals("<div>")){
+ logsError += "Aucune erreur n'est survenue durant la vérification du fichier.";
+ }
+ logsError += "</div>";
+
+ result.put("success", true);
+ }
+ catch(Exception e) {
+ result.put("success", false);
+ _log.error(e);
+ }
+ finally {
+ file.delete();
+ }
+
+ result.put("logs", logs);
+ result.put("logsError", logsError);
+ }
+ }
+
+ return result;
+ }
+
+ private static String formatLogLine(String[] fields){
+ String line = "<tr>";
+
+ for(int i=1; i<fields.length; i++){
+ String field = fields[i].trim();
+ line += "<td style='border:1px solid black; text-align: center;'>" + field + "</td>";
+ }
+
+ line += "</tr>";
+ return line;
+ }
+
+ private JSONObject importFile(ResourceRequest resourceRequest) throws NamingException, Exception {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);
+ User user = PortalUtil.getUser(resourceRequest);
+
+ long fileId = ParamUtil.getLong(resourceRequest, "fileId");
+ String importType = ParamUtil.getString(resourceRequest, "importType");
+
+ if(fileId>0){
+ String ou = OrganizationFinderServiceUtil.getEtabLDAPAttribute(OrganizationFinderServiceUtil.getEtabRatachement(user), "ou");
+
+ String commande = "";
+ if(importType.equals("enseignant")){
+ commande = PortletPropsValues.ENT_IMPORT_ENSEIGNANT_SCRIPT;
+ }
+ else if(importType.equals("eleve")){
+ commande = PortletPropsValues.ENT_IMPORT_ELEVE_SCRIPT;
+ }
+ else if(importType.equals("suivi")){
+ commande = PortletPropsValues.ENT_IMPORT_SUIVI_SCRIPT;
+ }
+
+ if(!commande.isEmpty()){
+
+ FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileId);
+ File file = FileUtil.createTempFile("xml");
+ File reportFile = FileUtil.createTempFile("zip");
+ FileUtil.write(file, fileEntry.getContentStream());
+
+ String reportPath = PortletPropsValues.ENT_IMPORT_REPORT_PATH + new Date().getTime() + "/";
+
+ String params = " --context_param companyId=" + user.getCompanyId() +
+ " --context_param etabId=" + ou +
+ " --context_param orgRootPortalMembersName=" + PortletPropsValues.ENT_IMPORT_ORG_ROOT_PORTAL_MEMBERS_NAME +
+ " --context_param reportPath=" + reportPath +
+ " --context_param file=" + file.getAbsolutePath();
+
+ commande += params;
+
+ System.out.println("Commande : \n" + commande);
+
+ String[] cmd = commande.split(" ");
+ try {
+ final Process p = Runtime.getRuntime().exec(cmd);
+
+ new Thread() {
+ public void run() {
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line = "";
+ try {
+ while((line = reader.readLine()) != null) {
+ System.out.println(line);
+ }
+ } finally {
+ reader.close();
+ }
+ } catch(IOException ioe) {
+ _log.error(ioe);
+ }
+ }
+ }.start();
+
+ new Thread() {
+ public void run() {
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
+ String line = "";
+ try {
+ while((line = reader.readLine()) != null) {
+ System.out.println(line);
+ }
+ } finally {
+ reader.close();
+ }
+ } catch(IOException ioe) {
+ _log.error(ioe);
+ }
+ }
+ }.start();
+
+ p.waitFor();
+
+ // Generation du zip
+ String commandeZip = "zip -r -j " + reportFile.getAbsolutePath() + " " + reportPath + " -i *.xls";
+ String[] cmdZip = commandeZip.split(" ");
+ final Process pZip = Runtime.getRuntime().exec(cmdZip);
+ new Thread() {
+ public void run() {
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(pZip.getErrorStream()));
+ String line = "";
+ try {
+ while((line = reader.readLine()) != null) {
+ System.out.println(line);
+ }
+ } finally {
+ reader.close();
+ }
+ } catch(IOException ioe) {
+ _log.error(ioe);
+ }
+ }
+ }.start();
+ pZip.waitFor();
+
+ String fileName = "Rapport_Import_" + importType + "_" + frenchDateFormat.format(new Date()) + ".zip";
+ String folderName = DLAppUtil.sendToDropbox(user, FileUtil.getBytes(reportFile), fileName);
+ String hrefFile = "<a target='_blank' href='" + themeDisplay.getURLPortal().toString() + "/" + themeDisplay.getPathMain() + "/document_library/get_file?p_l_id=" + themeDisplay.getPlid() + folderName + "'> <br /> <br />" + fileName +"</a>";
+
+ result.put("msg", "Un rapport de l'export au format zip a été déposé dans votre casier : " + hrefFile);
+
+ result.put("success", true);
+ }
+ catch(Exception e) {
+ result.put("success", false);
+ _log.error(e);
+ }
+ finally {
+ file.delete();
+ reportFile.delete();
+ DLAppLocalServiceUtil.deleteFileEntry(fileId);
+ FileUtil.deltree(reportPath);
+ }
+ }
+ }
+
+ return result;
+ }
+
+ protected void include(
+ String path, RenderRequest renderRequest,
+ RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ PortletRequestDispatcher portletRequestDispatcher =
+ getPortletContext().getRequestDispatcher(path);
+
+ if (portletRequestDispatcher == null) {
+ _log.error(path + " is not a valid include");
+ }
+ else {
+ portletRequestDispatcher.include(renderRequest, renderResponse);
+ }
+ }
+
+ protected String editJSP;
+ protected String helpJSP;
+ protected String viewJSP;
+
+ private static Log _log = LogFactoryUtil.getLog(JSPPortlet.class);
+
+}
\ No newline at end of file