Android Open Source - andro_auto_framework Download Csv






From Project

Back to project page andro_auto_framework.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCT...

If you think the Android project andro_auto_framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.//  w w w .  j a  va 2  s .c  o m
 */
package com.imaginea.botbot.server.servlet;

import com.imaginea.botbot.server.jpa.RecordEntry;
import com.imaginea.botbot.server.jpa.RecordSession;
import com.imaginea.botbot.server.service.PersistenceService;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.*;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.WebApplicationException;
import org.json.simple.JSONObject;

import org.json.simple.parser.JSONParser;

/**
 *
 * @author moiz
 * 
 * This servlet provides csv file for download
 * provided  paramater id which points to session id
 */
public class DownloadCsv extends HttpServlet {

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {

        ServletOutputStream out = null;
        ByteArrayInputStream byteArrayInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            
            
            //TODO if id is invalid it needs to be handles 
            int id = Integer.parseInt(request.getParameter("id"));
            RecordSession session = getEntity(id);
            List<RecordEntry> sessionList = session.getRecordEntries();
            List<RecordEntry> sortedList=getSortedlist(sessionList);
            Iterator<RecordEntry> iterator = sortedList.iterator();
            
            response.setContentType("text/csv");
            String disposition = "attachment; fileName=testCase.csv";
            response.setHeader("Content-Disposition", disposition);

            PrintWriter writer = response.getWriter();
            writer.append("\"Command\",\"Argument 1\",\"Argument 2\",\"Argument 3\"\n");

            while (iterator.hasNext()) {
                RecordEntry tempEntry = iterator.next();

                JSONObject jsonObject = (JSONObject) new JSONParser().parse(tempEntry.getPayload());

                if (jsonObject.containsKey("args[2]")) {
                    writer.append("\"" + jsonObject.get("command") + "\"" + "," + "\"" + jsonObject.get("args[0]") + "\"" + "," + "\"" + jsonObject.get("args[1]") + "\"" + "," + "\"" + jsonObject.get("args[2]") + "\"" + "\n");
                }else if (jsonObject.containsKey("args[1]")) {
                    writer.append("\"" + jsonObject.get("command") + "\"" + "," + "\"" + jsonObject.get("args[0]") + "\"" + "," + "\"" + jsonObject.get("args[1]") + "\"" + "\n");
                } else if (jsonObject.containsKey("args[0]")) {
                    writer.append("\"" + jsonObject.get("command") + "\"" + "," + "\"" + jsonObject.get("args[0]") + "\"" + "\n");
                }else{
                    writer.append("\"" + jsonObject.get("command") + "\"" + ","+ "\n");
                }
            }

            out.flush();
            out.close();

        } catch (Exception e) {
            System.err.println(e);


        } finally {
            if (out != null) {
                out.close();
            }
            if (byteArrayInputStream != null) {
                byteArrayInputStream.close();
            }
            if (bufferedOutputStream != null) {
                bufferedOutputStream.close();
            }
        }

    }
    
    //Getting a entity object
    protected RecordSession getEntity(long id) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        try {
            return (RecordSession) em.createQuery("SELECT e FROM RecordSession e where e.id = :id").setParameter("id", id).getSingleResult();
        } catch (NoResultException ex) {
            throw new WebApplicationException(new Throwable("Resource does not exist."), 404);
        }
    }
   
    private List<RecordEntry> getSortedlist(List<RecordEntry> original) {
        ArrayList<String> sortedArray = new ArrayList<String>();
        HashMap<String, String> source = new HashMap<String, String>();
        HashMap<String, RecordEntry> entryMap = new HashMap<String, RecordEntry>();
        List<RecordEntry> sortedEntry= new ArrayList<RecordEntry>();
        for(RecordEntry entry:original){
            source.put(entry.getEntryNo(), entry.getPrevEntryNo());
            entryMap.put(entry.getEntryNo(), entry);
        }
        ArrayList<String> desired = getDesiredMap(source, "0");
        while (!desired.isEmpty()) {
            sortedArray.add(desired.get(0));
            desired = getDesiredMap(source, desired.get(0));
        }
        for(String entryNo:sortedArray){
            RecordEntry entry=entryMap.get(entryNo);
            sortedEntry.add(entry);
        }
        
        return sortedEntry;
    }

    private ArrayList<String> getDesiredMap(HashMap<String, String> map, String entry) {
        Set<String> keys = map.keySet();
        ArrayList<String> temp = new ArrayList<String>();
        for (String key : keys) {
            if (map.get(key).contentEquals(entry)) {
                temp.add(key);
                temp.add(map.get(key));
                break;
            }
        }
        return temp;

    }
}




Java Source Code List

com.imaginea.botbot.server.converter.RecordEntriesConverter.java
com.imaginea.botbot.server.converter.RecordEntryConverter.java
com.imaginea.botbot.server.converter.RecordSessionConverter.java
com.imaginea.botbot.server.converter.RecordSessionsConverter.java
com.imaginea.botbot.server.converter.UriResolver.java
com.imaginea.botbot.server.jpa.RecordEntry.java
com.imaginea.botbot.server.jpa.RecordSession.java
com.imaginea.botbot.server.service.PersistenceService.java
com.imaginea.botbot.server.service.RecordEntriesResource.java
com.imaginea.botbot.server.service.RecordEntryResource.java
com.imaginea.botbot.server.service.RecordSessionResource.java
com.imaginea.botbot.server.service.RecordSessionsResource.java
com.imaginea.botbot.server.servlet.DownloadCsv.java
com.zutubi.android.junitreport.JUnitReportListener.java
com.zutubi.android.junitreport.JUnitReportTestRunner.java
com.zutubi.android.junitreport.TestKeeper.java
com.zutubi.android.junitreport.TestngReportListener.java
org.imaginea.botbot.CommandTransmitter.java
org.imaginea.botbot.Command.java
org.imaginea.botbot.Convertor.java
org.imaginea.botbot.CustomVisitor.java
org.imaginea.botbot.ListenerAdder.java
org.imaginea.botbot.OnClickListenerTest.java
org.imaginea.botbot.OnItemClickListenerTest.java
org.imaginea.botbot.OnItemSelectedListenerTest.java
org.imaginea.botbot.OnTouchListenerTest.java
org.imaginea.botbot.Recorder.java
org.imaginea.botbot.ServerProperties.java
org.imaginea.botbot.TextListner.java
org.imaginea.botbot.ViewClasses.java
org.imaginea.botbot.api.DefaultProperties.java
org.imaginea.botbot.api.IdentifyByType.java
org.imaginea.botbot.api.TestCSVReader.java
org.imaginea.botbot.api.UsefulFunctions.java
org.imaginea.botbot.common.AndroFrameworkExecutorDataDriven.java
org.imaginea.botbot.common.AndroFrameworkExecutor.java
org.imaginea.botbot.common.BaseClass.java
org.imaginea.botbot.common.BotBotTestRunner.java
org.imaginea.botbot.common.CommandExecutor.java
org.imaginea.botbot.common.Command.java
org.imaginea.botbot.common.DataDrivenTestCase.java
org.imaginea.botbot.common.Prefrences.java
org.imaginea.botbot.common.RobotiumBaseClass.java
org.imaginea.botbot.common.TestCaseGenerator.java
org.imaginea.botbot.filereader.BaseReader.java
org.imaginea.botbot.filereader.FileTypeReader.java
org.imaginea.botbot.filereader.PropertiesReader.java
org.imaginea.botbot.keywords.BaseKeywordDefinitions.java
org.imaginea.botbot.keywords.DynamicExecution.java
org.imaginea.botbot.keywords.IKeywords.java
org.imaginea.botbot.keywords.KeywordCaller.java
org.imaginea.botbot.keywords.NativeDriverKeywordDefinitions.java
org.imaginea.botbot.keywords.RobotiumKeywordDefinition.java
org.imaginea.botbot.utility.DataDrivenDataGenerator.java
org.imaginea.botbot.utility.WebViewHandler.java