package com.xpn.xwiki.web;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.plugin.packaging.PackageAPI;
import com.xpn.xwiki.plugin.packaging.DocumentInfo;
import com.xpn.xwiki.plugin.packaging.DocumentInfoAPI;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ldubost
 * Date: 11 dec. 2006
 * Time: 17:18:05
 * To change this template use File | Settings | File Templates.
 */
public class ImportAction extends XWikiAction {

    public String render(XWikiContext context) throws XWikiException {
        try {
            XWikiRequest request = context.getRequest();
            XWikiResponse response = context.getResponse();
            XWikiDocument doc = context.getDoc();
            String name =  request.get("name");
            String action =  request.get("action");
            String[] pages = request.getParameterValues("pages");

            if (!context.getWiki().getRightService().hasAdminRights(context)) {
                context.put("message", "needadminrights");
                return "exception";
            }

            if (name==null) {
                return "import";
            }

            PackageAPI importer = ((PackageAPI) context.getWiki().getPluginApi("package", context));

            // list files
            if ("getPackageInfos".equals(action)) {
                response.setContentType("text/xml");
                XWikiAttachment packFile = doc.getAttachment(name);
                importer.Import(packFile.getContent(context));
                String xml = importer.toXml();
                response.setContentLength(xml.getBytes().length);
                response.getWriter().write(xml);
                return null;
            } else if ("import".equals(action)) {
                XWikiAttachment packFile = doc.getAttachment(name);
                importer.Import(packFile.getContent(context));
                String all = request.get("all");
                if (!"1".equals(all)) {
                    if (pages!=null) {
                        List filelist = importer.getFiles();
                        for (int i=0;i<filelist.size();i++) {
                            DocumentInfoAPI dia = (DocumentInfoAPI) filelist.get(i);
                            dia.setAction(DocumentInfo.ACTION_SKIP);
                        }

                        for (int i=0;i<pages.length;i++) {
                            String pageName = pages[i];
                            String language = request.get("language_" + pageName);
                            String actionName =  "action_" + pageName;
                            if (language!=null)
                             actionName = "_" + language;
                            String defaultAction = request.get(actionName);
                            int iAction;
                            if ((defaultAction==null)||(defaultAction.equals(""))) {
                                iAction = DocumentInfo.ACTION_OVERWRITE;

                            } else {
                                try {
                                    iAction = Integer.parseInt(defaultAction);
                                } catch (Exception e) {
                                    iAction = DocumentInfo.ACTION_SKIP;
                                }
                            }

                            String docName = pageName.replaceAll(":.*$", "");
                            if (language==null)
                             importer.setDocumentAction(docName, iAction);
                            else
                             importer.setDocumentAction(docName, language, iAction);
                        }
                    }
                    // Import files
                    importer.install();
                    return "import";
                }
            }
        } catch (Exception e) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                    XWikiException.ERROR_XWIKI_APP_EXPORT,
                    "Exception while importing", e);
        }
        return null;
    }
}

@@ -402,20 +402,16 @@
 iJ IDEA.
-%0D%0A *
  User: l
@@ -416,20 +416,16 @@
  ldubost
-%0D%0A *
  Date: 1
@@ -435,20 +435,16 @@
 ec. 2006
-%0D%0A *
  Time: 1
@@ -450,20 +450,16 @@
 17:18:05
-%0D%0A *
  To chan
@@ -474,16 +474,20 @@
 template
+%0D%0A *
  use Fil
@@ -568,17 +568,18 @@
 kiAction
- 
+%0D%0A
 %7B%0D%0A%0D%0A   
@@ -643,16 +643,21 @@
 xception
+%0D%0A   
  %7B%0D%0A    
@@ -862,17 +862,16 @@
 g name =
- 
  request
@@ -912,17 +912,16 @@
 action =
- 
  request
@@ -1221,18 +1221,20 @@
 if (name
+ 
 ==
+ 
 null) %7B%0D
@@ -1318,16 +1318,33 @@
 porter =
+%0D%0A               
  ((Packa
@@ -2172,18 +2172,20 @@
 f (pages
+ 
 !=
+ 
 null) %7B%0D
@@ -2281,21 +2281,26 @@
 r (int i
-=0;i%3C
+ = 0; i %3C 
 filelist
@@ -2307,16 +2307,17 @@
 .size();
+ 
 i++) %7B%0D%0A
@@ -2539,13 +2539,18 @@
 nt i
-=0;i%3C
+ = 0; i %3C 
 page
@@ -2558,16 +2558,17 @@
 .length;
+ 
 i++) %7B%0D%0A
@@ -2755,17 +2755,16 @@
 onName =
- 
  %22action
@@ -2823,15 +2823,19 @@
 uage
+ 
 !=
+ 
 null)
+ %7B
 %0D%0A  
@@ -2861,16 +2861,19 @@
         
+   
 actionNa
@@ -2890,24 +2890,55 @@
  language;%0D%0A
+                            %7D%0D%0A
             
@@ -3094,17 +3094,21 @@
 tion
+ 
 ==
+ 
 null)
+ 
 %7C%7C
+ 
 (def
@@ -3208,18 +3208,16 @@
 WRITE;%0D%0A
-%0D%0A
         
@@ -3686,17 +3686,24 @@
 uage
+ 
 ==
+ 
 null)
-%0D%0A
+ %7B%0D%0A   
     
@@ -3801,21 +3801,25 @@
         
+ %7D
  else
+ %7B
 %0D%0A      
@@ -3833,32 +3833,35 @@
                 
+ 
+  
 importer.setDocu
@@ -3893,32 +3893,63 @@
 age, iAction);%0D%0A
+                            %7D%0D%0A
                 
@@ -4248,28 +4248,24 @@
             
-    
 XWikiExcepti
@@ -4308,20 +4308,16 @@
         
-    
 %22Excepti
@@ -4336,16 +4336,33 @@
 orting%22,
+%0D%0A               
  e);%0D%0A  

Apply codestyle