Update of 
/var/cvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/community/forms
In directory 
james.mmbase.org:/tmp/cvs-serv15371/src/java/com/finalist/cmsc/community/forms

Modified Files:
      Tag: b1_5
        ReferenceImportExportAction.java 
        ReferenceImportUploadForm.java 
Log Message:
CMSC-1265 - Community: improve import and export options of community users


See also: 
http://cvs.mmbase.org/viewcvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/community/forms
See also: http://www.mmbase.org/jira/browse/CMSC-1265


Index: ReferenceImportExportAction.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/community/forms/ReferenceImportExportAction.java,v
retrieving revision 1.7
retrieving revision 1.7.2.1
diff -u -b -r1.7 -r1.7.2.1
--- ReferenceImportExportAction.java    24 Sep 2008 07:29:37 -0000      1.7
+++ ReferenceImportExportAction.java    23 Feb 2009 15:34:21 -0000      1.7.2.1
@@ -1,10 +1,18 @@
 package com.finalist.cmsc.community.forms;
 
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -15,11 +23,24 @@
 import org.apache.struts.action.ActionMessages;
 import org.apache.struts.actions.DispatchAction;
 import org.apache.struts.upload.FormFile;
+import org.supercsv.cellprocessor.ConvertNullTo;
+import org.supercsv.cellprocessor.constraint.NotNull;
+import org.supercsv.cellprocessor.constraint.StrRegEx;
+import org.supercsv.cellprocessor.ift.CellProcessor;
+import org.supercsv.exception.SuperCSVReflectionException;
+import org.supercsv.io.CsvBeanReader;
+import org.supercsv.io.CsvBeanWriter;
+import org.supercsv.io.ICsvBeanReader;
+import org.supercsv.io.ICsvBeanWriter;
+import org.supercsv.prefs.CsvPreference;
+
 import com.finalist.cmsc.services.community.domain.CommunityExport;
+import com.finalist.cmsc.services.community.domain.CommunityExportForCsvVO;
 import com.finalist.cmsc.services.community.domain.PersonExportImportVO;
 import com.finalist.cmsc.services.community.person.PersonService;
 import com.finalist.cmsc.services.community.preferences.Preference;
 import com.finalist.cmsc.services.community.security.Authentication;
+import com.finalist.cmsc.services.community.security.Authority;
 import com.thoughtworks.xstream.XStream;
 import com.thoughtworks.xstream.io.xml.DomDriver;
 
@@ -30,19 +51,50 @@
 public class ReferenceImportExportAction extends DispatchAction {
    private static Log log = 
LogFactory.getLog(ReferenceImportExportAction.class);
    private PersonService personService;
+   static final CellProcessor[] userProcessors = new CellProcessor[] { null, 
new NotNull(), new NotNull(), null, null,
+         null, null, null, null, null, new NotNull(new 
StrRegEx("^[a-za-z0-9._%+...@[a-za-z0-9.-]+\\.[a-za-z]{2,6}$")),
+         null };
+
+   public ActionForward listGroups(ActionMapping mapping, ActionForm form, 
HttpServletRequest request,
+         HttpServletResponse response) throws IOException {
+      List<Authority> groups = personService.getAllAuthorities();
+      request.setAttribute("groups", groups);
+      return mapping.findForward("show");
+   }
+
+   public ActionForward showImportPage(ActionMapping mapping, ActionForm form, 
HttpServletRequest request,
+         HttpServletResponse response) throws IOException {
+      String groupId = request.getParameter("imGroupId");
+      request.setAttribute("groupId", groupId);
+      return mapping.findForward("failed");
+   }
 
    /**
-    * @param mapping do nothing 
-    * @param form do nothing 
-    * @param request do nothing 
-    * @param response setContentType
+    * @param mapping
+    *           do nothing
+    * @param form
+    *           do nothing
+    * @param request
+    *           do nothing
+    * @param response
+    *           setContentType
     * @return ActionForward do nothing 
-    * @throws IOException toXML
+    * @throws IOException
+    *            toXML
     */
    public ActionForward export(ActionMapping mapping, ActionForm form, 
HttpServletRequest request,
          HttpServletResponse response) throws IOException {
       log.debug("Export Susbscriptions");
-      CommunityExport communityExport = new 
CommunityExport(personService.getPersonExportImportVO());
+      ReferenceImportUploadForm groupForm = (ReferenceImportUploadForm) form;
+      String groupId = groupForm.getGroups();
+      CommunityExport communityExport;
+      if (StringUtils.equalsIgnoreCase("0", groupId)) {
+         communityExport = new 
CommunityExport(personService.getPersonExportImportVO());
+      } else {
+         communityExport = new 
CommunityExport(personService.getPersonExportImportVO(groupId));
+      }
+      String fileType = request.getParameter("fileType");
+      if (StringUtils.equalsIgnoreCase("xml", fileType)) {
       String xml = getXStream().toXML(communityExport);
       byte[] bytes = xml.getBytes();
       response.setContentType("text/xml");
@@ -53,48 +105,201 @@
       OutputStream outStream = response.getOutputStream();
       outStream.write(bytes);
       outStream.flush();
+      } else if (StringUtils.equalsIgnoreCase("csv", fileType)) {
+         response.setContentType("text/csv");
+         response.setHeader("Content-Disposition", "attachment; 
filename=subscriptions.csv");
+         response.setHeader("Cache-Control", "no-store");
+         ICsvBeanWriter writer = new CsvBeanWriter(response.getWriter(), 
CsvPreference.EXCEL_PREFERENCE);
+         String[] header = new String[] { "authenticationId", 
"authenticationUserId", "authenticationPassword",
+               "preferenceId", "preferenceModule", "preferenceKey", 
"preferenceValue", "firstName", "lastName",
+               "infix", "email", "active" };
+         ConvertNullTo constraint = new ConvertNullTo("");
+         CellProcessor[] processor = new CellProcessor[header.length];
+         for (int i = 0; i < processor.length; i++) {
+            processor[i] = constraint;
+         }
+         List<CommunityExportForCsvVO> communityCsvVos = 
fillCommunityVO(communityExport);
+         try {
+            writer.writeHeader(header);
+            for (CommunityExportForCsvVO communityCsvVo : communityCsvVos) {
+               writer.write(communityCsvVo, header, processor);
+            }
+         } catch (SuperCSVReflectionException e) {
+            e.printStackTrace();
+         } finally {
+            writer.close();
+         }
+      }
       return mapping.findForward(null);
    }
 
+   private List<CommunityExportForCsvVO> fillCommunityVO(CommunityExport 
communityExport) {
+      List<CommunityExportForCsvVO> communityCsvVos = new 
ArrayList<CommunityExportForCsvVO>();
+      for (PersonExportImportVO personExImVO : communityExport.getUsers()) {
+         Authentication auth = personExImVO.getAuthentication();
+         if (0 != personExImVO.getPreferences().size()) {
+            for (Preference preference : personExImVO.getPreferences()) {
+               CommunityExportForCsvVO communityVO = 
fillBasicCommunityExForVO(personExImVO, auth);
+               communityVO.setPreferenceId(preference.getId().toString());
+               communityVO.setPreferenceKey(preference.getKey());
+               communityVO.setPreferenceModule(preference.getModule());
+               communityVO.setPreferenceValue(preference.getValue());
+               communityCsvVos.add(communityVO);
+            }
+         } else {
+            CommunityExportForCsvVO communityVO = 
fillBasicCommunityExForVO(personExImVO, auth);
+            communityCsvVos.add(communityVO);
+         }
+      }
+      return communityCsvVos;
+   }
+
+   private CommunityExportForCsvVO 
fillBasicCommunityExForVO(PersonExportImportVO personExImVO, Authentication 
auth) {
+      CommunityExportForCsvVO communityVO = new CommunityExportForCsvVO();
+      communityVO.setAuthenticationId(auth.getId().toString());
+      communityVO.setAuthenticationPassword(auth.getPassword());
+      communityVO.setAuthenticationUserId(auth.getUserId());
+      communityVO.setActive(personExImVO.getActive());
+      communityVO.setFirstName(personExImVO.getFirstName());
+      communityVO.setInfix(personExImVO.getInfix());
+      communityVO.setLastName(personExImVO.getLastName());
+      communityVO.setEmail(personExImVO.getEmail());
+      return communityVO;
+   }
+
    /**
-    * @param mapping goto jsp
-    * @param form get paramate
-    * @param request save massages
-    * @param response do nothing
+    * @param mapping
+    *           goto jsp
+    * @param form
+    *           get paramate
+    * @param request
+    *           save massages
+    * @param response
+    *           do nothing
     * @return ActionForward chose jsp
-    * @throws IOException about treating XML
+    * @throws IOException
+    *            about treating XML
     */
    public ActionForward importsubscription(ActionMapping mapping, ActionForm 
form, HttpServletRequest request,
          HttpServletResponse response) throws IOException {
+      String groupId = request.getParameter("groupId");
       ReferenceImportUploadForm myForm = (ReferenceImportUploadForm) form;
-      FormFile myFile = myForm.getDatafile();
+      FormFile myFile = myForm.getFile();
       byte[] fileData = myFile.getFileData();
       String contentType = myFile.getContentType();
       boolean isXML = "text/xml".equals(contentType);
+      boolean isCSV = "text/csv".equals(contentType);
       String level = myForm.getLevel();
       ActionMessages messages = new ActionMessages();
-      if (!isXML) {
+      if (!isXML && !isCSV) {
          messages.add("file", new ActionMessage("datafile.unsupport"));
          saveMessages(request, messages);
+         request.setAttribute("warning", 1);
          return mapping.findForward("failed");
       }
+
+      if (isXML) {
       try {
-         importFromFile(fileData, level);
+            int size = importFromFile(fileData, level);
+            request.setAttribute("confirm_userNum", size);
       } catch (Exception e) {
          log.error(e);
          messages.add("file", new ActionMessage("datafile.invalid"));
          saveMessages(request, messages);
+            request.setAttribute("warning", 1);
+            request.setAttribute("groupId", groupId);
          return mapping.findForward("failed");
       }
-
+      } else if (isCSV) {
+         InputStreamReader ir = new InputStreamReader(myFile.getInputStream());
+         ICsvBeanReader inFile = new CsvBeanReader(ir, 
CsvPreference.EXCEL_PREFERENCE);
+         try {
+            String[] header = inFile.getCSVHeader(true);
+            if (header.length != userProcessors.length) {
+               ir = new InputStreamReader(myFile.getInputStream());
+               inFile = new CsvBeanReader(ir, 
CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
+               header = inFile.getCSVHeader(true);
+            }
+            Map<String, PersonExportImportVO> personVOMap = 
fillPersonVO(groupId, inFile, header);
+            if ("clean".equals(level)) {
+               batchCleanRecord();
+            }
+            Iterator<String> it = personVOMap.keySet().iterator();
+            while (it.hasNext()) {
+               personService.addRelationRecord(level, 
personVOMap.get(it.next()));
+            }
+            request.setAttribute("confirm_userNum", personVOMap.size());
+         } catch (Exception e) {
+            log.error(e);
+            messages.add("file", new ActionMessage("datafile.invalid"));
+            saveMessages(request, messages);
+            request.setAttribute("warning", 1);
+            request.setAttribute("groupId", groupId);
+            return mapping.findForward("failed");
+         } finally {
+            inFile.close();
+         }
+      }
       return mapping.findForward("success");
    }
 
-   private void importFromFile(byte[] fileData, String level) throws Exception 
{
+   private Map<String, PersonExportImportVO> fillPersonVO(String groupId, 
ICsvBeanReader inFile, String[] header)
+         throws IOException {
+      CommunityExportForCsvVO communityVO;
+      Map<String, PersonExportImportVO> personVOMap = new HashMap<String, 
PersonExportImportVO>();
+      while ((communityVO = inFile.read(CommunityExportForCsvVO.class, header, 
userProcessors)) != null) {
+         PersonExportImportVO personExImVO = new PersonExportImportVO();
+         Authentication auth = new Authentication();
+         List<Preference> preferences = new ArrayList<Preference>();
+         String authUserId = communityVO.getAuthenticationUserId();
+         if (personVOMap.containsKey(authUserId)) {
+            Preference pre = addPreference(communityVO);
+            personVOMap.get(authUserId).getPreferences().add(pre);
+         } else {
+            if (StringUtils.isNotEmpty(communityVO.getAuthenticationId())) {
+               auth.setId(new Long(communityVO.getAuthenticationId()));
+            }
+            auth.setUserId(communityVO.getAuthenticationUserId());
+            auth.setPassword(communityVO.getAuthenticationPassword());
+            personExImVO.setAuthentication(auth);
+            personExImVO.setFirstName(communityVO.getFirstName());
+            personExImVO.setLastName(communityVO.getLastName());
+            personExImVO.setInfix(communityVO.getInfix());
+            personExImVO.setEmail(communityVO.getEmail());
+            personExImVO.setRegisterDate(new Date(System.currentTimeMillis()));
+            if (null != communityVO.getActive()) {
+               personExImVO.setActive(communityVO.getActive());
+            } else {
+               personExImVO.setActive("active");
+            }
+            Preference pre = addPreference(communityVO);
+            preferences.add(pre);
+            personExImVO.setPreferences(preferences);
+            if (null != groupId && groupId != "0" && groupId != "") {
+               personExImVO.setAuthorityId(new Long(groupId));
+            }
+            personVOMap.put(authUserId, personExImVO);
+         }
+      }
+      return personVOMap;
+   }
+
+   private Preference addPreference(CommunityExportForCsvVO communityVO) {
+      Preference pre = new Preference();
+      if (StringUtils.isNotEmpty(communityVO.getPreferenceId())) {
+         pre.setId(new Long(communityVO.getPreferenceId()));
+      }
+      pre.setKey(communityVO.getPreferenceKey());
+      pre.setModule(communityVO.getPreferenceModule());
+      pre.setValue(communityVO.getPreferenceValue());
+      return pre;
+   }
+
+   private int importFromFile(byte[] fileData, String level) throws Exception {
       String xml = new String(fileData);
       CommunityExport communityExport;
       communityExport = (CommunityExport) getXStream().fromXML(xml);
-      List < PersonExportImportVO > xpersons = communityExport.getUsers();
+      List<PersonExportImportVO> xpersons = communityExport.getUsers();
       if ("clean".equals(level)) {
          batchCleanRecord();
       }
@@ -106,6 +311,7 @@
          }
          personService.addRelationRecord(level, importPerson);
       }
+      return xpersons.size();
    }
 
    private void batchCleanRecord() throws Exception {


Index: ReferenceImportUploadForm.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer_Modules/community/src/java/com/finalist/cmsc/community/forms/ReferenceImportUploadForm.java,v
retrieving revision 1.2
retrieving revision 1.2.2.1
diff -u -b -r1.2 -r1.2.2.1
--- ReferenceImportUploadForm.java      23 Sep 2008 07:12:56 -0000      1.2
+++ ReferenceImportUploadForm.java      23 Feb 2009 15:34:21 -0000      1.2.2.1
@@ -6,7 +6,19 @@
 public class ReferenceImportUploadForm extends ActionForm {
 
    private FormFile datafile;
+   private FormFile file;
    private String level;
+   private String group;
+   private String groups;
+   
+
+   public String getGroups() {
+      return groups;
+   }
+
+   public void setGroups(String groups) {
+      this.groups = groups;
+   }
 
    public String getLevel() {
       return level;
@@ -23,4 +35,20 @@
    public void setDatafile(FormFile datafile) {
       this.datafile = datafile;
    }
+
+   public String getGroup() {
+      return group;
+   }
+
+   public void setGroup(String group) {
+      this.group = group;
+   }
+
+   public FormFile getFile() {
+      return file;
+   }
+
+   public void setFile(FormFile file) {
+      this.file = file;
+   }
 }
\ No newline at end of file
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to