List of usage examples for java.util.zip ZipOutputStream close
public void close() throws IOException
From source file:com.portfolio.data.provider.MysqlAdminProvider.java
@Override public Object getPortfolio(MimeType outMimeType, String portfolioUuid, int userId, int groupId, String label, String resource, String files) throws Exception { String rootNodeUuid = getPortfolioRootNode(portfolioUuid); String header = ""; String footer = ""; NodeRight nodeRight = credential.getPortfolioRight(userId, groupId, portfolioUuid, Credential.READ); if (!nodeRight.read) return "faux"; if (outMimeType.getSubType().equals("xml")) { // header = "<portfolio xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' schemaVersion='1.0'>"; // footer = "</portfolio>"; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; Document document = null; try {//from ww w . j a v a 2s . c o m documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.newDocument(); document.setXmlStandalone(true); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } Element root = document.createElement("portfolio"); root.setAttribute("id", portfolioUuid); root.setAttribute("code", "0"); //// Noeuds supplmentaire pour WAD // Version Element verNode = document.createElement("version"); Text version = document.createTextNode("3"); verNode.appendChild(version); root.appendChild(verNode); // metadata-wad Element metawad = document.createElement("metadata-wad"); metawad.setAttribute("prog", "main.jsp"); metawad.setAttribute("owner", "N"); root.appendChild(metawad); // root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); // root.setAttribute("schemaVersion", "1.0"); document.appendChild(root); getLinearXml(portfolioUuid, rootNodeUuid, root, true, null, userId, groupId); StringWriter stw = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.transform(new DOMSource(document), new StreamResult(stw)); if (resource != null && files != null) { if (resource.equals("true") && files.equals("true")) { String adressedufichier = System.getProperty("user.dir") + "/tmp_getPortfolio_" + new Date() + ".xml"; String adresseduzip = System.getProperty("user.dir") + "/tmp_getPortfolio_" + new Date() + ".zip"; File file = null; PrintWriter ecrire; PrintWriter ecri; try { file = new File(adressedufichier); ecrire = new PrintWriter(new FileOutputStream(adressedufichier)); ecrire.println(stw.toString()); ecrire.flush(); ecrire.close(); System.out.print("fichier cree "); } catch (IOException ioe) { System.out.print("Erreur : "); ioe.printStackTrace(); } try { String fileName = portfolioUuid + ".zip"; ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(adresseduzip)); zip.setMethod(ZipOutputStream.DEFLATED); zip.setLevel(Deflater.BEST_COMPRESSION); File dataDirectories = new File(file.getName()); FileInputStream fis = new FileInputStream(dataDirectories); byte[] bytes = new byte[fis.available()]; fis.read(bytes); ZipEntry entry = new ZipEntry(file.getName()); entry.setTime(dataDirectories.lastModified()); zip.putNextEntry(entry); zip.write(bytes); zip.closeEntry(); fis.close(); //zipDirectory(dataDirectories, zip); zip.close(); file.delete(); return adresseduzip; } catch (FileNotFoundException fileNotFound) { fileNotFound.printStackTrace(); } catch (IOException io) { io.printStackTrace(); } } } return stw.toString(); } else if (outMimeType.getSubType().equals("json")) { header = "{\"portfolio\": { \"-xmlns:xsi\": \"http://www.w3.org/2001/XMLSchema-instance\",\"-schemaVersion\": \"1.0\","; footer = "}}"; } return header + getNode(outMimeType, rootNodeUuid, true, userId, groupId, label).toString() + footer; }
From source file:com.portfolio.rest.RestServicePortfolio.java
@Path("/portfolios/portfolio/{portfolio-id}") @GET// w w w . ja va2 s .c o m @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "application/zip", MediaType.APPLICATION_OCTET_STREAM }) public Object getPortfolio(@CookieParam("user") String user, @CookieParam("credential") String token, @QueryParam("group") int groupId, @PathParam("portfolio-id") String portfolioUuid, @Context ServletConfig sc, @Context HttpServletRequest httpServletRequest, @HeaderParam("Accept") String accept, @QueryParam("user") Integer userId, @QueryParam("group") Integer group, @QueryParam("resources") String resource, @QueryParam("files") String files, @QueryParam("export") String export, @QueryParam("lang") String lang) { UserInfo ui = checkCredential(httpServletRequest, user, token, null); Response response = null; try { String portfolio = dataProvider.getPortfolio(new MimeType("text/xml"), portfolioUuid, ui.userId, 0, this.label, resource, "", ui.subId).toString(); if ("faux".equals(portfolio)) { response = Response.status(403).build(); } if (response == null) { Date time = new Date(); Document doc = DomUtils.xmlString2Document(portfolio, new StringBuffer()); NodeList codes = doc.getDocumentElement().getElementsByTagName("code"); // Le premier c'est celui du root Node codenode = codes.item(0); String code = ""; if (codenode != null) code = codenode.getTextContent(); if (export != null) { response = Response.ok(portfolio).header("content-disposition", "attachment; filename = \"" + code + "-" + time + ".xml\"").build(); } else if (resource != null && files != null) { //// Cas du renvoi d'un ZIP /// Temp file in temp directory File tempDir = new File(System.getProperty("java.io.tmpdir", null)); File tempZip = File.createTempFile(portfolioUuid, ".zip", tempDir); FileOutputStream fos = new FileOutputStream(tempZip); ZipOutputStream zos = new ZipOutputStream(fos); // BufferedOutputStream bos = new BufferedOutputStream(zos); /// zos.setComment("Some comment"); /// Write xml file to zip ZipEntry ze = new ZipEntry(portfolioUuid + ".xml"); zos.putNextEntry(ze); byte[] bytes = portfolio.getBytes("UTF-8"); zos.write(bytes); zos.closeEntry(); /// Find all fileid/filename XPath xPath = XPathFactory.newInstance().newXPath(); String filterRes = "//asmResource/fileid"; NodeList nodelist = (NodeList) xPath.compile(filterRes).evaluate(doc, XPathConstants.NODESET); /// Direct link to data // String urlTarget = "http://"+ server + "/user/" + user +"/file/" + uuid +"/"+ lang+ "/ptype/fs"; /* String langatt = ""; if( lang != null ) langatt = "?lang="+lang; else langatt = "?lang=fr"; //*/ /// Fetch all files for (int i = 0; i < nodelist.getLength(); ++i) { Node res = nodelist.item(i); Node p = res.getParentNode(); // resource -> container Node gp = p.getParentNode(); // container -> context Node uuidNode = gp.getAttributes().getNamedItem("id"); String uuid = uuidNode.getTextContent(); String filterName = "./filename[@lang and text()]"; NodeList textList = (NodeList) xPath.compile(filterName).evaluate(p, XPathConstants.NODESET); String filename = ""; if (textList.getLength() != 0) { Element fileNode = (Element) textList.item(0); filename = fileNode.getTextContent(); lang = fileNode.getAttribute("lang"); if ("".equals(lang)) lang = "fr"; } String servlet = httpServletRequest.getRequestURI(); servlet = servlet.substring(0, servlet.indexOf("/", 7)); String server = httpServletRequest.getServerName(); int port = httpServletRequest.getServerPort(); // "http://"+ server + /resources/resource/file/ uuid ? lang= size= // String urlTarget = "http://"+ server + "/user/" + user +"/file/" + uuid +"/"+ lang+ "/ptype/fs"; String url = "http://" + server + ":" + port + servlet + "/resources/resource/file/" + uuid + "?lang=" + lang; HttpGet get = new HttpGet(url); // Transfer sessionid so that local request still get security checked HttpSession session = httpServletRequest.getSession(true); get.addHeader("Cookie", "JSESSIONID=" + session.getId()); // Send request CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse ret = client.execute(get); HttpEntity entity = ret.getEntity(); // Put specific name for later recovery if ("".equals(filename)) continue; int lastDot = filename.lastIndexOf("."); if (lastDot < 0) lastDot = 0; String filenameext = filename.substring(0); /// find extension int extindex = filenameext.lastIndexOf("."); filenameext = uuid + "_" + lang + filenameext.substring(extindex); // Save it to zip file // int length = (int) entity.getContentLength(); InputStream content = entity.getContent(); // BufferedInputStream bis = new BufferedInputStream(entity.getContent()); ze = new ZipEntry(filenameext); try { int totalread = 0; zos.putNextEntry(ze); int inByte; byte[] buf = new byte[4096]; // zos.write(bytes,0,inByte); while ((inByte = content.read(buf)) != -1) { totalread += inByte; zos.write(buf, 0, inByte); } System.out.println("FILE: " + filenameext + " -> " + totalread); content.close(); // bis.close(); zos.closeEntry(); } catch (Exception e) { e.printStackTrace(); } EntityUtils.consume(entity); ret.close(); client.close(); } zos.close(); fos.close(); /// Return zip file RandomAccessFile f = new RandomAccessFile(tempZip.getAbsoluteFile(), "r"); byte[] b = new byte[(int) f.length()]; f.read(b); f.close(); response = Response.ok(b, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename = \"" + code + "-" + time + ".zip") .build(); // Temp file cleanup tempZip.delete(); } else { //try { this.userId = userId; } catch(Exception ex) { this.userId = -1; }; // String returnValue = dataProvider.getPortfolio(new MimeType("text/xml"),portfolioUuid,this.userId, this.groupId, this.label, resource, files).toString(); if (portfolio.equals("faux")) { throw new RestWebApplicationException(Status.FORBIDDEN, "Vous n'avez pas les droits necessaires"); } if (accept.equals(MediaType.APPLICATION_JSON)) { portfolio = XML.toJSONObject(portfolio).toString(); response = Response.ok(portfolio).type(MediaType.APPLICATION_JSON).build(); } else response = Response.ok(portfolio).type(MediaType.APPLICATION_XML).build(); logRestRequest(httpServletRequest, null, portfolio, Status.OK.getStatusCode()); } } } catch (RestWebApplicationException ex) { throw new RestWebApplicationException(Status.FORBIDDEN, ex.getResponse().getEntity().toString()); } catch (SQLException ex) { logRestRequest(httpServletRequest, null, "Portfolio " + portfolioUuid + " not found", Status.NOT_FOUND.getStatusCode()); throw new RestWebApplicationException(Status.NOT_FOUND, "Portfolio " + portfolioUuid + " not found"); } catch (Exception ex) { ex.printStackTrace(); logRestRequest(httpServletRequest, null, ex.getMessage() + "\n\n" + ex.getStackTrace(), Status.INTERNAL_SERVER_ERROR.getStatusCode()); throw new RestWebApplicationException(Status.INTERNAL_SERVER_ERROR, ex.getMessage()); } finally { if (dataProvider != null) dataProvider.disconnect(); } return response; }
From source file:fr.cirad.mgdb.exporting.markeroriented.HapMapExportHandler.java
@Override public void exportData(OutputStream outputStream, String sModule, List<SampleId> sampleIDs, ProgressIndicator progress, DBCursor markerCursor, Map<Comparable, Comparable> markerSynonyms, int nMinimumGenotypeQuality, int nMinimumReadDepth, Map<String, InputStream> readyToExportFiles) throws Exception { MongoTemplate mongoTemplate = MongoTemplateManager.get(sModule); File warningFile = File.createTempFile("export_warnings_", ""); FileWriter warningFileWriter = new FileWriter(warningFile); int markerCount = markerCursor.count(); ZipOutputStream zos = new ZipOutputStream(outputStream); if (readyToExportFiles != null) for (String readyToExportFile : readyToExportFiles.keySet()) { zos.putNextEntry(new ZipEntry(readyToExportFile)); InputStream inputStream = readyToExportFiles.get(readyToExportFile); byte[] dataBlock = new byte[1024]; int count = inputStream.read(dataBlock, 0, 1024); while (count != -1) { zos.write(dataBlock, 0, count); count = inputStream.read(dataBlock, 0, 1024); }/*from ww w. j a v a 2 s. co m*/ } List<Individual> individuals = getIndividualsFromSamples(sModule, sampleIDs); ArrayList<String> individualList = new ArrayList<String>(); for (int i = 0; i < sampleIDs.size(); i++) { Individual individual = individuals.get(i); if (!individualList.contains(individual.getId())) { individualList.add(individual.getId()); } } String exportName = sModule + "_" + markerCount + "variants_" + individualList.size() + "individuals"; zos.putNextEntry(new ZipEntry(exportName + ".hapmap")); String header = "rs#" + "\t" + "alleles" + "\t" + "chrom" + "\t" + "pos" + "\t" + "strand" + "\t" + "assembly#" + "\t" + "center" + "\t" + "protLSID" + "\t" + "assayLSID" + "\t" + "panelLSID" + "\t" + "QCcode"; zos.write(header.getBytes()); for (int i = 0; i < individualList.size(); i++) { zos.write(("\t" + individualList.get(i)).getBytes()); } zos.write((LINE_SEPARATOR).getBytes()); int avgObjSize = (Integer) mongoTemplate .getCollection(mongoTemplate.getCollectionName(VariantRunData.class)).getStats().get("avgObjSize"); int nChunkSize = nMaxChunkSizeInMb * 1024 * 1024 / avgObjSize; short nProgress = 0, nPreviousProgress = 0; long nLoadedMarkerCount = 0; while (markerCursor == null || markerCursor.hasNext()) { int nLoadedMarkerCountInLoop = 0; Map<Comparable, String> markerChromosomalPositions = new LinkedHashMap<Comparable, String>(); boolean fStartingNewChunk = true; markerCursor.batchSize(nChunkSize); while (markerCursor.hasNext() && (fStartingNewChunk || nLoadedMarkerCountInLoop % nChunkSize != 0)) { DBObject exportVariant = markerCursor.next(); DBObject refPos = (DBObject) exportVariant.get(VariantData.FIELDNAME_REFERENCE_POSITION); markerChromosomalPositions.put((Comparable) exportVariant.get("_id"), refPos.get(ReferencePosition.FIELDNAME_SEQUENCE) + ":" + refPos.get(ReferencePosition.FIELDNAME_START_SITE)); nLoadedMarkerCountInLoop++; fStartingNewChunk = false; } List<Comparable> currentMarkers = new ArrayList<Comparable>(markerChromosomalPositions.keySet()); LinkedHashMap<VariantData, Collection<VariantRunData>> variantsAndRuns = MgdbDao.getSampleGenotypes( mongoTemplate, sampleIDs, currentMarkers, true, null /*new Sort(VariantData.FIELDNAME_REFERENCE_POSITION + "." + ChromosomalPosition.FIELDNAME_SEQUENCE).and(new Sort(VariantData.FIELDNAME_REFERENCE_POSITION + "." + ChromosomalPosition.FIELDNAME_START_SITE))*/); // query mongo db for matching genotypes for (VariantData variant : variantsAndRuns.keySet()) // read data and write results into temporary files (one per sample) { Comparable variantId = variant.getId(); if (markerSynonyms != null) { Comparable syn = markerSynonyms.get(variantId); if (syn != null) variantId = syn; } boolean fIsSNP = variant.getType().equals(Type.SNP.toString()); byte[] missingGenotype = ("\t" + "NN").getBytes(); String[] chromAndPos = markerChromosomalPositions.get(variant.getId()).split(":"); zos.write(((variantId == null ? variant.getId() : variantId) + "\t" + StringUtils.join(variant.getKnownAlleleList(), "/") + "\t" + chromAndPos[0] + "\t" + Long.parseLong(chromAndPos[1]) + "\t" + "+").getBytes()); for (int j = 0; j < 6; j++) zos.write(("\t" + "NA").getBytes()); Map<String, Integer> gqValueForSampleId = new LinkedHashMap<String, Integer>(); Map<String, Integer> dpValueForSampleId = new LinkedHashMap<String, Integer>(); Map<String, List<String>> individualGenotypes = new LinkedHashMap<String, List<String>>(); Collection<VariantRunData> runs = variantsAndRuns.get(variant); if (runs != null) for (VariantRunData run : runs) for (Integer sampleIndex : run.getSampleGenotypes().keySet()) { SampleGenotype sampleGenotype = run.getSampleGenotypes().get(sampleIndex); String gtCode = run.getSampleGenotypes().get(sampleIndex).getCode(); String individualId = individuals .get(sampleIDs.indexOf(new SampleId(run.getId().getProjectId(), sampleIndex))) .getId(); List<String> storedIndividualGenotypes = individualGenotypes.get(individualId); if (storedIndividualGenotypes == null) { storedIndividualGenotypes = new ArrayList<String>(); individualGenotypes.put(individualId, storedIndividualGenotypes); } storedIndividualGenotypes.add(gtCode); gqValueForSampleId.put(individualId, (Integer) sampleGenotype.getAdditionalInfo().get(VariantData.GT_FIELD_GQ)); dpValueForSampleId.put(individualId, (Integer) sampleGenotype.getAdditionalInfo().get(VariantData.GT_FIELD_DP)); } int writtenGenotypeCount = 0; for (String individualId : individualList /* we use this list because it has the proper ordering */) { int individualIndex = individualList.indexOf(individualId); while (writtenGenotypeCount < individualIndex - 1) { zos.write(missingGenotype); writtenGenotypeCount++; } List<String> genotypes = individualGenotypes.get(individualId); HashMap<Object, Integer> genotypeCounts = new HashMap<Object, Integer>(); // will help us to keep track of missing genotypes int highestGenotypeCount = 0; String mostFrequentGenotype = null; if (genotypes != null) for (String genotype : genotypes) { if (genotype.length() == 0) continue; /* skip missing genotypes */ Integer gqValue = gqValueForSampleId.get(individualId); if (gqValue != null && gqValue < nMinimumGenotypeQuality) continue; /* skip this sample because its GQ is under the threshold */ Integer dpValue = dpValueForSampleId.get(individualId); if (dpValue != null && dpValue < nMinimumReadDepth) continue; /* skip this sample because its DP is under the threshold */ int gtCount = 1 + MgdbDao.getCountForKey(genotypeCounts, genotype); if (gtCount > highestGenotypeCount) { highestGenotypeCount = gtCount; mostFrequentGenotype = genotype; } genotypeCounts.put(genotype, gtCount); } byte[] exportedGT = mostFrequentGenotype == null ? missingGenotype : ("\t" + StringUtils.join(variant.getAllelesFromGenotypeCode(mostFrequentGenotype), fIsSNP ? "" : "/")).getBytes(); zos.write(exportedGT); writtenGenotypeCount++; if (genotypeCounts.size() > 1) warningFileWriter.write("- Dissimilar genotypes found for variant " + (variantId == null ? variant.getId() : variantId) + ", individual " + individualId + ". Exporting most frequent: " + new String(exportedGT) + "\n"); } while (writtenGenotypeCount < individualList.size()) { zos.write(missingGenotype); writtenGenotypeCount++; } zos.write((LINE_SEPARATOR).getBytes()); } if (progress.hasAborted()) return; nLoadedMarkerCount += nLoadedMarkerCountInLoop; nProgress = (short) (nLoadedMarkerCount * 100 / markerCount); if (nProgress > nPreviousProgress) { // if (nProgress%5 == 0) // LOG.info("========================= exportData: " + nProgress + "% =========================" + (System.currentTimeMillis() - before)/1000 + "s"); progress.setCurrentStepProgress(nProgress); nPreviousProgress = nProgress; } } warningFileWriter.close(); if (warningFile.length() > 0) { zos.putNextEntry(new ZipEntry(exportName + "-REMARKS.txt")); int nWarningCount = 0; BufferedReader in = new BufferedReader(new FileReader(warningFile)); String sLine; while ((sLine = in.readLine()) != null) { zos.write((sLine + "\n").getBytes()); in.readLine(); nWarningCount++; } LOG.info("Number of Warnings for export (" + exportName + "): " + nWarningCount); in.close(); } warningFile.delete(); zos.close(); progress.setCurrentStepProgress((short) 100); }
From source file:fr.cirad.mgdb.exporting.markeroriented.EigenstratExportHandler.java
@Override public void exportData(OutputStream outputStream, String sModule, List<SampleId> sampleIDs, ProgressIndicator progress, DBCursor markerCursor, Map<Comparable, Comparable> markerSynonyms, int nMinimumGenotypeQuality, int nMinimumReadDepth, Map<String, InputStream> readyToExportFiles) throws Exception { // long before = System.currentTimeMillis(); File warningFile = File.createTempFile("export_warnings_", ""); FileWriter warningFileWriter = new FileWriter(warningFile); File snpFile = null;/*from w ww . j a va 2s. com*/ try { snpFile = File.createTempFile("snpFile", ""); FileWriter snpFileWriter = new FileWriter(snpFile); ZipOutputStream zos = new ZipOutputStream(outputStream); if (ByteArrayOutputStream.class.isAssignableFrom(outputStream.getClass())) zos.setLevel(ZipOutputStream.STORED); if (readyToExportFiles != null) for (String readyToExportFile : readyToExportFiles.keySet()) { zos.putNextEntry(new ZipEntry(readyToExportFile)); InputStream inputStream = readyToExportFiles.get(readyToExportFile); byte[] dataBlock = new byte[1024]; int count = inputStream.read(dataBlock, 0, 1024); while (count != -1) { zos.write(dataBlock, 0, count); count = inputStream.read(dataBlock, 0, 1024); } } MongoTemplate mongoTemplate = MongoTemplateManager.get(sModule); int markerCount = markerCursor.count(); List<Individual> individuals = getIndividualsFromSamples(sModule, sampleIDs); ArrayList<String> individualList = new ArrayList<String>(); StringBuffer indFileContents = new StringBuffer(); for (int i = 0; i < sampleIDs.size(); i++) { Individual individual = individuals.get(i); if (!individualList.contains(individual.getId())) { individualList.add(individual.getId()); indFileContents .append(individual.getId() + "\t" + getIndividualGenderCode(sModule, individual.getId()) + "\t" + (individual.getPopulation() == null ? "." : individual.getPopulation()) + LINE_SEPARATOR); } } String exportName = sModule + "_" + markerCount + "variants_" + individualList.size() + "individuals"; zos.putNextEntry(new ZipEntry(exportName + ".ind")); zos.write(indFileContents.toString().getBytes()); zos.putNextEntry(new ZipEntry(exportName + ".eigenstratgeno")); int avgObjSize = (Integer) mongoTemplate .getCollection(mongoTemplate.getCollectionName(VariantRunData.class)).getStats() .get("avgObjSize"); int nChunkSize = nMaxChunkSizeInMb * 1024 * 1024 / avgObjSize; short nProgress = 0, nPreviousProgress = 0; long nLoadedMarkerCount = 0; while (markerCursor.hasNext()) { int nLoadedMarkerCountInLoop = 0; Map<Comparable, String> markerChromosomalPositions = new LinkedHashMap<Comparable, String>(); boolean fStartingNewChunk = true; markerCursor.batchSize(nChunkSize); while (markerCursor.hasNext() && (fStartingNewChunk || nLoadedMarkerCountInLoop % nChunkSize != 0)) { DBObject exportVariant = markerCursor.next(); DBObject refPos = (DBObject) exportVariant.get(VariantData.FIELDNAME_REFERENCE_POSITION); markerChromosomalPositions.put((Comparable) exportVariant.get("_id"), refPos.get(ReferencePosition.FIELDNAME_SEQUENCE) + ":" + refPos.get(ReferencePosition.FIELDNAME_START_SITE)); nLoadedMarkerCountInLoop++; fStartingNewChunk = false; } List<Comparable> currentMarkers = new ArrayList<Comparable>(markerChromosomalPositions.keySet()); LinkedHashMap<VariantData, Collection<VariantRunData>> variantsAndRuns = MgdbDao.getSampleGenotypes( mongoTemplate, sampleIDs, currentMarkers, true, null /*new Sort(VariantData.FIELDNAME_REFERENCE_POSITION + "." + ChromosomalPosition.FIELDNAME_SEQUENCE).and(new Sort(VariantData.FIELDNAME_REFERENCE_POSITION + "." + ChromosomalPosition.FIELDNAME_START_SITE))*/); // query mongo db for matching genotypes for (VariantData variant : variantsAndRuns.keySet()) // read data and write results into temporary files (one per sample) { Comparable variantId = variant.getId(); List<String> chromAndPos = Helper.split(markerChromosomalPositions.get(variantId), ":"); if (chromAndPos.size() == 0) LOG.warn("Chromosomal position not found for marker " + variantId); // LOG.debug(marker + "\t" + (chromAndPos.length == 0 ? "0" : chromAndPos[0]) + "\t" + 0 + "\t" + (chromAndPos.length == 0 ? 0l : Long.parseLong(chromAndPos[1])) + LINE_SEPARATOR); if (markerSynonyms != null) { Comparable syn = markerSynonyms.get(variantId); if (syn != null) variantId = syn; } snpFileWriter.write(variantId + "\t" + (chromAndPos.size() == 0 ? "0" : chromAndPos.get(0)) + "\t" + 0 + "\t" + (chromAndPos.size() == 0 ? 0l : Long.parseLong(chromAndPos.get(1))) + LINE_SEPARATOR); Map<String, List<String>> individualGenotypes = new LinkedHashMap<String, List<String>>(); Collection<VariantRunData> runs = variantsAndRuns.get(variant); if (runs != null) for (VariantRunData run : runs) for (Integer sampleIndex : run.getSampleGenotypes().keySet()) { SampleGenotype sampleGenotype = run.getSampleGenotypes().get(sampleIndex); String individualId = individuals .get(sampleIDs .indexOf(new SampleId(run.getId().getProjectId(), sampleIndex))) .getId(); Integer gq = null; try { gq = (Integer) sampleGenotype.getAdditionalInfo().get(VariantData.GT_FIELD_GQ); } catch (Exception ignored) { } if (gq != null && gq < nMinimumGenotypeQuality) continue; Integer dp = null; try { dp = (Integer) sampleGenotype.getAdditionalInfo().get(VariantData.GT_FIELD_DP); } catch (Exception ignored) { } if (dp != null && dp < nMinimumReadDepth) continue; String gtCode = sampleGenotype.getCode(); List<String> storedIndividualGenotypes = individualGenotypes.get(individualId); if (storedIndividualGenotypes == null) { storedIndividualGenotypes = new ArrayList<String>(); individualGenotypes.put(individualId, storedIndividualGenotypes); } storedIndividualGenotypes.add(gtCode); } for (int j = 0; j < individualList .size(); j++ /* we use this list because it has the proper ordering*/) { String individualId = individualList.get(j); List<String> genotypes = individualGenotypes.get(individualId); HashMap<Object, Integer> genotypeCounts = new HashMap<Object, Integer>(); // will help us to keep track of missing genotypes int highestGenotypeCount = 0; String mostFrequentGenotype = null; if (genotypes != null) for (String genotype : genotypes) { if (genotype.length() == 0) continue; /* skip missing genotypes */ int gtCount = 1 + MgdbDao.getCountForKey(genotypeCounts, genotype); if (gtCount > highestGenotypeCount) { highestGenotypeCount = gtCount; mostFrequentGenotype = genotype; } genotypeCounts.put(genotype, gtCount); } List<String> alleles = mostFrequentGenotype == null ? new ArrayList<String>() : variant.getAllelesFromGenotypeCode(mostFrequentGenotype); int nOutputCode = 0; if (mostFrequentGenotype == null) nOutputCode = 9; else for (String all : Helper.split(mostFrequentGenotype, "/")) if ("0".equals(all)) nOutputCode++; if (j == 0 && variant.getKnownAlleleList().size() > 2) warningFileWriter.write("- Variant " + variant.getId() + " is multi-allelic. Make sure Eigenstrat genotype encoding specifications are suitable for you.\n"); zos.write(("" + nOutputCode).getBytes()); if (genotypeCounts.size() > 1 || alleles.size() > 2) { if (genotypeCounts.size() > 1) warningFileWriter.write("- Dissimilar genotypes found for variant " + (variantId == null ? variant.getId() : variantId) + ", individual " + individualId + ". Exporting most frequent: " + nOutputCode + "\n"); if (alleles.size() > 2) warningFileWriter.write("- More than 2 alleles found for variant " + (variantId == null ? variant.getId() : variantId) + ", individual " + individualId + ". Exporting only the first 2 alleles.\n"); } } zos.write((LINE_SEPARATOR).getBytes()); } if (progress.hasAborted()) return; nLoadedMarkerCount += nLoadedMarkerCountInLoop; nProgress = (short) (nLoadedMarkerCount * 100 / markerCount); if (nProgress > nPreviousProgress) { // if (nProgress%5 == 0) // LOG.info("============= exportData: " + nProgress + "% =============" + (System.currentTimeMillis() - before)/1000 + "s"); progress.setCurrentStepProgress(nProgress); nPreviousProgress = nProgress; } } snpFileWriter.close(); zos.putNextEntry(new ZipEntry(exportName + ".snp")); BufferedReader in = new BufferedReader(new FileReader(snpFile)); String sLine; while ((sLine = in.readLine()) != null) zos.write((sLine + "\n").getBytes()); in.close(); warningFileWriter.close(); if (warningFile.length() > 0) { zos.putNextEntry(new ZipEntry(exportName + "-REMARKS.txt")); int nWarningCount = 0; in = new BufferedReader(new FileReader(warningFile)); while ((sLine = in.readLine()) != null) { zos.write((sLine + "\n").getBytes()); nWarningCount++; } LOG.info("Number of Warnings for export (" + exportName + "): " + nWarningCount); in.close(); } warningFile.delete(); zos.close(); progress.setCurrentStepProgress((short) 100); } finally { if (snpFile != null && snpFile.exists()) snpFile.delete(); } }
From source file:fr.cirad.mgdb.exporting.markeroriented.GFFExportHandler.java
@Override public void exportData(OutputStream outputStream, String sModule, List<SampleId> sampleIDs, ProgressIndicator progress, DBCursor markerCursor, Map<Comparable, Comparable> markerSynonyms, int nMinimumGenotypeQuality, int nMinimumReadDepth, Map<String, InputStream> readyToExportFiles) throws Exception { MongoTemplate mongoTemplate = MongoTemplateManager.get(sModule); ZipOutputStream zos = new ZipOutputStream(outputStream); if (readyToExportFiles != null) for (String readyToExportFile : readyToExportFiles.keySet()) { zos.putNextEntry(new ZipEntry(readyToExportFile)); InputStream inputStream = readyToExportFiles.get(readyToExportFile); byte[] dataBlock = new byte[1024]; int count = inputStream.read(dataBlock, 0, 1024); while (count != -1) { zos.write(dataBlock, 0, count); count = inputStream.read(dataBlock, 0, 1024); }//from w ww . j a v a 2 s .c om } File warningFile = File.createTempFile("export_warnings_", ""); FileWriter warningFileWriter = new FileWriter(warningFile); int markerCount = markerCursor.count(); List<Individual> individuals = getIndividualsFromSamples(sModule, sampleIDs); ArrayList<String> individualList = new ArrayList<String>(); for (int i = 0; i < sampleIDs.size(); i++) { Individual individual = individuals.get(i); if (!individualList.contains(individual.getId())) { individualList.add(individual.getId()); } } String exportName = sModule + "_" + markerCount + "variants_" + individualList.size() + "individuals"; zos.putNextEntry(new ZipEntry(exportName + ".gff3")); String header = "##gff-version 3" + LINE_SEPARATOR; zos.write(header.getBytes()); TreeMap<String, String> typeToOntology = new TreeMap<String, String>(); typeToOntology.put(Type.SNP.toString(), "SO:0000694"); typeToOntology.put(Type.INDEL.toString(), "SO:1000032"); typeToOntology.put(Type.MIXED.toString(), "SO:0001059"); typeToOntology.put(Type.SYMBOLIC.toString(), "SO:0000109"); typeToOntology.put(Type.MNP.toString(), "SO:0001059"); int avgObjSize = (Integer) mongoTemplate .getCollection(mongoTemplate.getCollectionName(VariantRunData.class)).getStats().get("avgObjSize"); int nChunkSize = nMaxChunkSizeInMb * 1024 * 1024 / avgObjSize; short nProgress = 0, nPreviousProgress = 0; long nLoadedMarkerCount = 0; while (markerCursor.hasNext()) { int nLoadedMarkerCountInLoop = 0; Map<Comparable, String> markerChromosomalPositions = new LinkedHashMap<Comparable, String>(); boolean fStartingNewChunk = true; markerCursor.batchSize(nChunkSize); while (markerCursor.hasNext() && (fStartingNewChunk || nLoadedMarkerCountInLoop % nChunkSize != 0)) { DBObject exportVariant = markerCursor.next(); DBObject refPos = (DBObject) exportVariant.get(VariantData.FIELDNAME_REFERENCE_POSITION); markerChromosomalPositions.put((Comparable) exportVariant.get("_id"), refPos.get(ReferencePosition.FIELDNAME_SEQUENCE) + ":" + refPos.get(ReferencePosition.FIELDNAME_START_SITE)); nLoadedMarkerCountInLoop++; fStartingNewChunk = false; } List<Comparable> currentMarkers = new ArrayList<Comparable>(markerChromosomalPositions.keySet()); LinkedHashMap<VariantData, Collection<VariantRunData>> variantsAndRuns = MgdbDao.getSampleGenotypes( mongoTemplate, sampleIDs, currentMarkers, true, null /*new Sort(VariantData.FIELDNAME_REFERENCE_POSITION + "." + ChromosomalPosition.FIELDNAME_SEQUENCE).and(new Sort(VariantData.FIELDNAME_REFERENCE_POSITION + "." + ChromosomalPosition.FIELDNAME_START_SITE))*/); // query mongo db for matching genotypes for (VariantData variant : variantsAndRuns.keySet()) // read data and write results into temporary files (one per sample) { Comparable variantId = variant.getId(); List<String> variantDataOrigin = new ArrayList<String>(); Map<String, Integer> gqValueForSampleId = new LinkedHashMap<String, Integer>(); Map<String, Integer> dpValueForSampleId = new LinkedHashMap<String, Integer>(); Map<String, List<String>> individualGenotypes = new LinkedHashMap<String, List<String>>(); List<String> chromAndPos = Helper.split(markerChromosomalPositions.get(variantId), ":"); if (chromAndPos.size() == 0) LOG.warn("Chromosomal position not found for marker " + variantId); // LOG.debug(marker + "\t" + (chromAndPos.length == 0 ? "0" : chromAndPos[0]) + "\t" + 0 + "\t" + (chromAndPos.length == 0 ? 0l : Long.parseLong(chromAndPos[1])) + LINE_SEPARATOR); if (markerSynonyms != null) { Comparable syn = markerSynonyms.get(variantId); if (syn != null) variantId = syn; } Collection<VariantRunData> runs = variantsAndRuns.get(variant); if (runs != null) for (VariantRunData run : runs) for (Integer sampleIndex : run.getSampleGenotypes().keySet()) { SampleGenotype sampleGenotype = run.getSampleGenotypes().get(sampleIndex); String individualId = individuals .get(sampleIDs.indexOf(new SampleId(run.getId().getProjectId(), sampleIndex))) .getId(); Integer gq = null; try { gq = (Integer) sampleGenotype.getAdditionalInfo().get(VariantData.GT_FIELD_GQ); } catch (Exception ignored) { } if (gq != null && gq < nMinimumGenotypeQuality) continue; Integer dp = null; try { dp = (Integer) sampleGenotype.getAdditionalInfo().get(VariantData.GT_FIELD_DP); } catch (Exception ignored) { } if (dp != null && dp < nMinimumReadDepth) continue; String gtCode = sampleGenotype.getCode(); List<String> storedIndividualGenotypes = individualGenotypes.get(individualId); if (storedIndividualGenotypes == null) { storedIndividualGenotypes = new ArrayList<String>(); individualGenotypes.put(individualId, storedIndividualGenotypes); } storedIndividualGenotypes.add(gtCode); } zos.write((chromAndPos.get(0) + "\t" + StringUtils.join(variantDataOrigin, ";") /*source*/ + "\t" + typeToOntology.get(variant.getType()) + "\t" + Long.parseLong(chromAndPos.get(1)) + "\t" + Long.parseLong(chromAndPos.get(1)) + "\t" + "." + "\t" + "+" + "\t" + "." + "\t") .getBytes()); Comparable syn = markerSynonyms == null ? null : markerSynonyms.get(variant.getId()); zos.write(("ID=" + variant.getId() + ";" + (syn != null ? "Name=" + syn + ";" : "") + "alleles=" + StringUtils.join(variant.getKnownAlleleList(), "/") + ";" + "refallele=" + variant.getKnownAlleleList().get(0) + ";").getBytes()); for (int j = 0; j < individualList .size(); j++ /* we use this list because it has the proper ordering*/) { NumberFormat nf = NumberFormat.getInstance(Locale.US); nf.setMaximumFractionDigits(4); HashMap<String, Integer> compt1 = new HashMap<String, Integer>(); int highestGenotypeCount = 0; int sum = 0; String individualId = individualList.get(j); List<String> genotypes = individualGenotypes.get(individualId); HashMap<Object, Integer> genotypeCounts = new HashMap<Object, Integer>(); // will help us to keep track of missing genotypes String mostFrequentGenotype = null; if (genotypes != null) for (String genotype : genotypes) { if (genotype.length() == 0) continue; /* skip missing genotypes */ int count = 0; for (String t : variant.getAllelesFromGenotypeCode(genotype)) { for (String t1 : variant.getKnownAlleleList()) { if (t.equals(t1) && !(compt1.containsKey(t1))) { count++; compt1.put(t1, count); } else if (t.equals(t1) && compt1.containsKey(t1)) { if (compt1.get(t1) != 0) { count++; compt1.put(t1, count); } else compt1.put(t1, count); } else if (!(compt1.containsKey(t1))) { compt1.put(t1, 0); } } } for (int countValue : compt1.values()) { sum += countValue; } int gtCount = 1 + MgdbDao.getCountForKey(genotypeCounts, genotype); if (gtCount > highestGenotypeCount) { highestGenotypeCount = gtCount; mostFrequentGenotype = genotype; } genotypeCounts.put(genotype, gtCount); } List<String> alleles = mostFrequentGenotype == null ? new ArrayList<String>() : variant.getAllelesFromGenotypeCode(mostFrequentGenotype); if (alleles.size() != 0) { zos.write(("acounts=" + individualId + ":").getBytes()); for (String knowAllelesCompt : compt1.keySet()) { zos.write( (knowAllelesCompt + " " + nf.format(compt1.get(knowAllelesCompt) / (float) sum) + " " + compt1.get(knowAllelesCompt) + " ").getBytes()); } zos.write((alleles.size() + ";").getBytes()); } if (genotypeCounts.size() > 1) { Comparable sVariantId = markerSynonyms != null ? markerSynonyms.get(variant.getId()) : variant.getId(); warningFileWriter.write("- Dissimilar genotypes found for variant " + (sVariantId == null ? variant.getId() : sVariantId) + ", individual " + individualId + ". Exporting most frequent: " + StringUtils.join(alleles, ",") + "\n"); } } zos.write((LINE_SEPARATOR).getBytes()); } if (progress.hasAborted()) return; nLoadedMarkerCount += nLoadedMarkerCountInLoop; nProgress = (short) (nLoadedMarkerCount * 100 / markerCount); if (nProgress > nPreviousProgress) { // if (nProgress%5 == 0) // LOG.info("========================= exportData: " + nProgress + "% =========================" + (System.currentTimeMillis() - before)/1000 + "s"); progress.setCurrentStepProgress(nProgress); nPreviousProgress = nProgress; } } warningFileWriter.close(); if (warningFile.length() > 0) { zos.putNextEntry(new ZipEntry(exportName + "-REMARKS.txt")); int nWarningCount = 0; BufferedReader in = new BufferedReader(new FileReader(warningFile)); String sLine; while ((sLine = in.readLine()) != null) { zos.write((sLine + "\n").getBytes()); in.readLine(); nWarningCount++; } LOG.info("Number of Warnings for export (" + exportName + "): " + nWarningCount); in.close(); } warningFile.delete(); zos.close(); progress.setCurrentStepProgress((short) 100); }
From source file:com.portfolio.data.provider.MysqlDataProvider.java
@Override public Object getPortfolio(MimeType outMimeType, String portfolioUuid, int userId, int groupId, String label, String resource, String files, int substid) throws Exception { String rootNodeUuid = getPortfolioRootNode(portfolioUuid); String header = ""; String footer = ""; NodeRight nodeRight = credential.getPortfolioRight(userId, groupId, portfolioUuid, Credential.READ); if (!nodeRight.read) { userId = credential.getPublicUid(); // NodeRight nodeRight = new NodeRight(false,false,false,false,false,false); /// Vrifie les droits avec le compte publique (dernire chance) nodeRight = credential.getPublicRight(userId, 123, rootNodeUuid, "dummy"); if (!nodeRight.read) return "faux"; }/*w w w . j av a 2 s.c om*/ if (outMimeType.getSubType().equals("xml")) { // header = "<portfolio xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' schemaVersion='1.0'>"; // footer = "</portfolio>"; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; Document document = null; try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.newDocument(); document.setXmlStandalone(true); } catch (ParserConfigurationException e) { e.printStackTrace(); } Element root = document.createElement("portfolio"); root.setAttribute("id", portfolioUuid); root.setAttribute("code", "0"); //// Noeuds supplmentaire pour WAD // Version Element verNode = document.createElement("version"); Text version = document.createTextNode("4"); verNode.appendChild(version); root.appendChild(verNode); // metadata-wad // Element metawad = document.createElement("metadata-wad"); // metawad.setAttribute("prog", "main.jsp"); // metawad.setAttribute("owner", "N"); // root.appendChild(metawad); int owner = credential.getOwner(userId, portfolioUuid); String isOwner = "N"; if (owner == userId) isOwner = "Y"; root.setAttribute("owner", isOwner); // root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); // root.setAttribute("schemaVersion", "1.0"); document.appendChild(root); getLinearXml(portfolioUuid, rootNodeUuid, root, true, null, userId, nodeRight.groupId, nodeRight.groupLabel); StringWriter stw = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.transform(new DOMSource(document), new StreamResult(stw)); if (resource != null && files != null) { if (resource.equals("true") && files.equals("true")) { String adressedufichier = System.getProperty("user.dir") + "/tmp_getPortfolio_" + new Date() + ".xml"; String adresseduzip = System.getProperty("user.dir") + "/tmp_getPortfolio_" + new Date() + ".zip"; File file = null; PrintWriter ecrire; try { file = new File(adressedufichier); ecrire = new PrintWriter(new FileOutputStream(adressedufichier)); ecrire.println(stw.toString()); ecrire.flush(); ecrire.close(); System.out.print("fichier cree "); } catch (IOException ioe) { System.out.print("Erreur : "); ioe.printStackTrace(); } try { ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(adresseduzip)); zip.setMethod(ZipOutputStream.DEFLATED); zip.setLevel(Deflater.BEST_COMPRESSION); File dataDirectories = new File(file.getName()); FileInputStream fis = new FileInputStream(dataDirectories); byte[] bytes = new byte[fis.available()]; fis.read(bytes); ZipEntry entry = new ZipEntry(file.getName()); entry.setTime(dataDirectories.lastModified()); zip.putNextEntry(entry); zip.write(bytes); zip.closeEntry(); fis.close(); //zipDirectory(dataDirectories, zip); zip.close(); file.delete(); return adresseduzip; } catch (FileNotFoundException fileNotFound) { fileNotFound.printStackTrace(); } catch (IOException io) { io.printStackTrace(); } } } return stw.toString(); } else if (outMimeType.getSubType().equals("json")) { header = "{\"portfolio\": { \"-xmlns:xsi\": \"http://www.w3.org/2001/XMLSchema-instance\",\"-schemaVersion\": \"1.0\","; footer = "}}"; } return header + getNode(outMimeType, rootNodeUuid, true, userId, groupId, label).toString() + footer; }
From source file:it.govpay.web.rs.dars.monitoraggio.pagamenti.PagamentiHandler.java
@Override public String esporta(Long idToExport, List<RawParamValue> rawValues, UriInfo uriInfo, BasicBD bd, ZipOutputStream zout) throws WebApplicationException, ConsoleException, ExportException { String methodName = "esporta " + this.titoloServizio + "[" + idToExport + "]"; Printer printer = null;//from w ww . j a v a 2 s . co m int numeroZipEntries = 0; boolean esportaCsv = false, esportaPdf = false, esportaRtPdf = false, esportaRtBase64 = false; try { String pathLoghi = ConsoleProperties.getInstance().getPathEstrattoContoPdfLoghi(); String fileName = "Pagamenti.zip"; this.log.info("Esecuzione " + methodName + " in corso..."); // Operazione consentita solo ai ruoli con diritto di lettura this.darsService.checkDirittiServizioLettura(bd, this.funzionalita); String esportaCsvId = Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esportaCsv.id"); String esportaCsvS = Utils.getValue(rawValues, esportaCsvId); if (StringUtils.isNotEmpty(esportaCsvS)) { esportaCsv = Boolean.parseBoolean(esportaCsvS); } String esportaPdfId = Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esportaPdf.id"); String esportaPdfS = Utils.getValue(rawValues, esportaPdfId); if (StringUtils.isNotEmpty(esportaPdfS)) { esportaPdf = Boolean.parseBoolean(esportaPdfS); } String esportaRtPdfId = Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esportaRtPdf.id"); String esportaRtPdfS = Utils.getValue(rawValues, esportaRtPdfId); if (StringUtils.isNotEmpty(esportaRtPdfS)) { esportaRtPdf = Boolean.parseBoolean(esportaRtPdfS); } String esportaRtBase64Id = Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esportaRtBase64.id"); String esportaRtBase64S = Utils.getValue(rawValues, esportaRtBase64Id); if (StringUtils.isNotEmpty(esportaRtBase64S)) { esportaRtBase64 = Boolean.parseBoolean(esportaRtBase64S); } // almeno una voce deve essere selezionata if (!(esportaCsv || esportaPdf || esportaRtPdf || esportaRtBase64)) { List<String> msg = new ArrayList<String>(); msg.add(Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esporta.tipiExportObbligatori")); throw new ExportException(msg, EsitoOperazione.ERRORE); } Set<Long> setDomini = this.darsService.getIdDominiAbilitatiLetturaServizio(bd, this.funzionalita); boolean eseguiRicerca = !setDomini.isEmpty(); it.govpay.core.business.EstrattoConto estrattoContoBD = new it.govpay.core.business.EstrattoConto(bd); PagamentiBD pagamentiBD = new PagamentiBD(bd); PagamentoFilter filter = pagamentiBD.newFilter(); filter.setSogliaRitardo(ConsoleProperties.getInstance().getSogliaGiorniRitardoPagamenti()); List<String> idDomini = new ArrayList<String>(); List<Long> idsToExport = new ArrayList<Long>(); idsToExport.add(idToExport); if (!setDomini.contains(-1L)) { List<Long> lstCodDomini = new ArrayList<Long>(); lstCodDomini.addAll(setDomini); idDomini.addAll(this.toListCodDomini(lstCodDomini, bd)); filter.setIdDomini(idDomini); // l'operatore puo' vedere i domini associati, controllo se c'e' un versamento con Id nei domini concessi. if (eseguiRicerca) { filter.setIdPagamenti(idsToExport); eseguiRicerca = eseguiRicerca && pagamentiBD.count(filter) > 0; } } if (eseguiRicerca) { Pagamento pagamento = pagamentiBD.getPagamento(idToExport); if (esportaRtPdf || esportaRtBase64) { // ricevuta pagamento try { Rpt rpt = pagamento.getRpt(bd); Dominio dominio = pagamento.getDominio(bd); if (rpt.getXmlRt() != null) { String ricevutaFileName = dominio.getCodDominio() + "_" + pagamento.getIuv() + "_" + pagamento.getIur(); if (esportaRtPdf) { Versamento versamento = rpt.getVersamento(bd); // RT in formato pdf String tipoFirma = rpt.getFirmaRichiesta().getCodifica(); byte[] rtByteValidato = RtUtils.validaFirma(tipoFirma, rpt.getXmlRt(), dominio.getCodDominio()); CtRicevutaTelematica rt = JaxbUtils.toRT(rtByteValidato); ByteArrayOutputStream baos = new ByteArrayOutputStream(); String auxDigit = dominio.getAuxDigit() + ""; String applicationCode = String.format("%02d", dominio.getStazione(bd).getApplicationCode()); RicevutaPagamentoUtils.getPdfRicevutaPagamento(dominio.getLogo(), versamento.getCausaleVersamento(), rt, pagamento, auxDigit, applicationCode, baos, this.log); String rtPdfEntryName = ricevutaFileName + ".pdf"; numeroZipEntries++; ZipEntry rtPdf = new ZipEntry(rtPdfEntryName); zout.putNextEntry(rtPdf); zout.write(baos.toByteArray()); zout.closeEntry(); } if (esportaRtBase64) { // RT in formato Base 64 String rtBase64EntryName = ricevutaFileName + ".txt"; numeroZipEntries++; ZipEntry rtPdf = new ZipEntry(rtBase64EntryName); zout.putNextEntry(rtPdf); zout.write(rpt.getXmlRt()); zout.closeEntry(); } } } catch (Exception e) { } } List<Long> ids = new ArrayList<Long>(); if (pagamento.getIdSingoloVersamento() != null) ids.add(pagamento.getIdSingoloVersamento()); SingoliVersamentiBD singoliVersamentiBD = new SingoliVersamentiBD(bd); EstrattiContoBD estrattiContoBD = new EstrattiContoBD(bd); EstrattoContoFilter ecFilter = estrattiContoBD.newFilter(); if (esportaCsv) { // recupero oggetto ecFilter.setIdSingoloVersamento(ids); List<EstrattoConto> findAll = eseguiRicerca ? estrattiContoBD.estrattoContoFromIdSingoliVersamenti(ecFilter) : new ArrayList<EstrattoConto>(); if (findAll != null && findAll.size() > 0) { numeroZipEntries++; //ordinamento record Collections.sort(findAll, new EstrattoContoComparator()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ZipEntry pagamentoCsv = new ZipEntry("pagamenti.csv"); zout.putNextEntry(pagamentoCsv); printer = new Printer(this.getFormat(), baos); printer.printRecord(CSVUtils.getEstrattoContoCsvHeader()); for (EstrattoConto eConto : findAll) { printer.printRecord(CSVUtils.getEstrattoContoAsCsvRow(eConto, this.sdf)); } } finally { try { if (printer != null) { printer.close(); } } catch (Exception e) { throw new Exception("Errore durante la chiusura dello stream ", e); } } zout.write(baos.toByteArray()); zout.closeEntry(); } } if (esportaPdf) { SingoloVersamento singoloVersamento = singoliVersamentiBD .getSingoloVersamento(pagamento.getIdSingoloVersamento()); Versamento versamento = singoloVersamento.getVersamento(bd); UnitaOperativa uo = AnagraficaManager.getUnitaOperativa(bd, versamento.getIdUo()); Dominio dominio = AnagraficaManager.getDominio(bd, uo.getIdDominio()); // Estratto conto per iban e codiceversamento. List<Long> idSingoliVersamentiDominio = new ArrayList<Long>(); idSingoliVersamentiDominio.add(idToExport); it.govpay.core.business.model.EstrattoConto input = it.govpay.core.business.model.EstrattoConto .creaEstrattoContoPagamentiPDF(dominio, idSingoliVersamentiDominio); List<it.govpay.core.business.model.EstrattoConto> listInputEstrattoConto = new ArrayList<it.govpay.core.business.model.EstrattoConto>(); listInputEstrattoConto.add(input); List<it.govpay.core.business.model.EstrattoConto> listOutputEstattoConto = estrattoContoBD .getEstrattoContoPagamenti(listInputEstrattoConto, pathLoghi); for (it.govpay.core.business.model.EstrattoConto estrattoContoOutput : listOutputEstattoConto) { Map<String, ByteArrayOutputStream> estrattoContoVersamenti = estrattoContoOutput .getOutput(); for (String nomeEntry : estrattoContoVersamenti.keySet()) { numeroZipEntries++; ByteArrayOutputStream baos = estrattoContoVersamenti.get(nomeEntry); // ZipEntry estrattoContoEntry = new ZipEntry(estrattoContoOutput.getDominio().getCodDominio() + "/" + nomeEntry); ZipEntry estrattoContoEntry = new ZipEntry(nomeEntry); zout.putNextEntry(estrattoContoEntry); zout.write(baos.toByteArray()); zout.closeEntry(); } } } } // se non ho inserito nessuna entry if (numeroZipEntries == 0) { String noEntriesTxt = "/README"; ZipEntry entryTxt = new ZipEntry(noEntriesTxt); zout.putNextEntry(entryTxt); zout.write("Non sono state trovate informazioni sui pagamenti selezionati.".getBytes()); zout.closeEntry(); } zout.flush(); zout.close(); this.log.info("Esecuzione " + methodName + " completata."); return fileName; } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw new ConsoleException(e); } }
From source file:com.globalsight.webservices.Ambassador.java
private void compressionXml(String zipFileName, File inputFile) { try {//from w ww. j ava 2 s . c om ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); out.putNextEntry(new ZipEntry(inputFile.getName())); FileInputStream inputStream = new FileInputStream(inputFile); int b; while ((b = inputStream.read()) != -1) { out.write(b); } inputStream.close(); inputFile.delete(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:it.govpay.web.rs.dars.monitoraggio.pagamenti.PagamentiHandler.java
@Override public String esporta(List<Long> idsToExport, List<RawParamValue> rawValues, UriInfo uriInfo, BasicBD bd, ZipOutputStream zout) throws WebApplicationException, ConsoleException, ExportException { Printer printer = null;//ww w.j a v a 2 s . c om String methodName = "exportMassivo " + this.titoloServizio; int numeroZipEntries = 0; boolean esportaCsv = false, esportaPdf = false, esportaRtPdf = false, esportaRtBase64 = false; String fileName = "Pagamenti.zip"; try { String pathLoghi = ConsoleProperties.getInstance().getPathEstrattoContoPdfLoghi(); this.log.info("Esecuzione " + methodName + " in corso..."); // Operazione consentita solo ai ruoli con diritto di lettura this.darsService.checkDirittiServizioLettura(bd, this.funzionalita); int limit = ConsoleProperties.getInstance().getNumeroMassimoElementiExport(); boolean simpleSearch = Utils.containsParameter(rawValues, DarsService.SIMPLE_SEARCH_PARAMETER_ID); PagamentiBD pagamentiBD = new PagamentiBD(bd); PagamentoFilter filter = pagamentiBD.newFilter(simpleSearch); Map<String, String> params = new HashMap<String, String>(); String esportaCsvId = Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esportaCsv.id"); String esportaCsvS = Utils.getValue(rawValues, esportaCsvId); if (StringUtils.isNotEmpty(esportaCsvS)) { esportaCsv = Boolean.parseBoolean(esportaCsvS); } String esportaPdfId = Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esportaPdf.id"); String esportaPdfS = Utils.getValue(rawValues, esportaPdfId); if (StringUtils.isNotEmpty(esportaPdfS)) { esportaPdf = Boolean.parseBoolean(esportaPdfS); } String esportaRtPdfId = Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esportaRtPdf.id"); String esportaRtPdfS = Utils.getValue(rawValues, esportaRtPdfId); if (StringUtils.isNotEmpty(esportaRtPdfS)) { esportaRtPdf = Boolean.parseBoolean(esportaRtPdfS); } String esportaRtBase64Id = Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esportaRtBase64.id"); String esportaRtBase64S = Utils.getValue(rawValues, esportaRtBase64Id); if (StringUtils.isNotEmpty(esportaRtBase64S)) { esportaRtBase64 = Boolean.parseBoolean(esportaRtBase64S); } // almeno una voce deve essere selezionata if (!(esportaCsv || esportaPdf || esportaRtPdf || esportaRtBase64)) { List<String> msg = new ArrayList<String>(); msg.add(Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esporta.tipiExportObbligatori")); throw new ExportException(msg, EsitoOperazione.ERRORE); } // se ho ricevuto anche gli id li utilizzo per fare il check della count if (idsToExport != null && idsToExport.size() > 0) filter.setIdPagamenti(idsToExport); //1. eseguo una count per verificare che il numero dei risultati da esportare sia <= sogliamassimaexport massivo boolean eseguiRicerca = this.popolaFiltroPagamenti(rawValues, uriInfo, pagamentiBD, params, simpleSearch, filter); if (!eseguiRicerca) { List<String> msg = new ArrayList<String>(); msg.add(Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esporta.operazioneNonPermessa")); throw new ExportException(msg, EsitoOperazione.ERRORE); } long count = pagamentiBD.count(filter); if (count < 1) { List<String> msg = new ArrayList<String>(); msg.add(Utils.getInstance(this.getLanguage()) .getMessageFromResourceBundle(this.nomeServizio + ".esporta.nessunElementoDaEsportare")); throw new ExportException(msg, EsitoOperazione.ERRORE); } if (esportaRtPdf && count > ConsoleProperties.getInstance().getNumeroMassimoElementiExport()) { List<String> msg = new ArrayList<String>(); msg.add(Utils.getInstance(this.getLanguage()).getMessageFromResourceBundle( this.nomeServizio + ".esporta.numeroElementiDaEsportareSopraSogliaMassima")); throw new ExportException(msg, EsitoOperazione.ERRORE); } FilterSortWrapper fsw = new FilterSortWrapper(); fsw.setField(it.govpay.orm.Pagamento.model().DATA_PAGAMENTO); fsw.setSortOrder(SortOrder.DESC); filter.getFilterSortList().add(fsw); List<Pagamento> findAllPag = new ArrayList<Pagamento>(); int countIterazione = -1; int offset = 0; // esecuzione della ricerca di tutti i pagamenti paginata per problemi di performance, nel caso di esporta rt pdf c'e' il limit impostato e si fa solo un ciclo do { filter.setOffset(offset); filter.setLimit(limit); List<Pagamento> findAllPagTmp = pagamentiBD.findAll(filter); countIterazione = findAllPagTmp != null ? findAllPagTmp.size() : 0; offset += countIterazione; if (findAllPagTmp != null && findAllPagTmp.size() > 0) findAllPag.addAll(findAllPagTmp); } while (countIterazione > 0 && !esportaRtPdf); List<Long> ids = new ArrayList<Long>(); for (Pagamento pagamento : findAllPag) { if (pagamento.getIdSingoloVersamento() != null) ids.add(pagamento.getIdSingoloVersamento()); if (esportaRtPdf || esportaRtBase64) { // ricevuta pagamento try { Rpt rpt = pagamento.getRpt(bd); Dominio dominio = pagamento.getDominio(bd); if (rpt.getXmlRt() != null) { numeroZipEntries++; String ricevutaFileName = dominio.getCodDominio() + "_" + pagamento.getIuv() + "_" + pagamento.getIur(); if (esportaRtPdf) { Versamento versamento = rpt.getVersamento(bd); // RT in formato pdf String tipoFirma = rpt.getFirmaRichiesta().getCodifica(); byte[] rtByteValidato = RtUtils.validaFirma(tipoFirma, rpt.getXmlRt(), dominio.getCodDominio()); CtRicevutaTelematica rt = JaxbUtils.toRT(rtByteValidato); ByteArrayOutputStream baos = new ByteArrayOutputStream(); String auxDigit = dominio.getAuxDigit() + ""; String applicationCode = String.format("%02d", dominio.getStazione(bd).getApplicationCode()); RicevutaPagamentoUtils.getPdfRicevutaPagamento(dominio.getLogo(), versamento.getCausaleVersamento(), rt, pagamento, auxDigit, applicationCode, baos, this.log); String rtPdfEntryName = ricevutaFileName + ".pdf"; numeroZipEntries++; ZipEntry rtPdf = new ZipEntry(rtPdfEntryName); zout.putNextEntry(rtPdf); zout.write(baos.toByteArray()); zout.closeEntry(); } if (esportaRtBase64) { // RT in formato Base 64 String rtBase64EntryName = ricevutaFileName + ".txt"; numeroZipEntries++; ZipEntry rtPdf = new ZipEntry(rtBase64EntryName); zout.putNextEntry(rtPdf); zout.write(rpt.getXmlRt()); zout.closeEntry(); } } } catch (Exception e) { } } } if (esportaCsv) { EstrattiContoBD estrattiContoBD = new EstrattiContoBD(bd); EstrattoContoFilter ecFilter = estrattiContoBD.newFilter(); // recupero oggetto ecFilter.setIdSingoloVersamento(ids); List<EstrattoConto> findAll = eseguiRicerca ? estrattiContoBD.estrattoContoFromIdSingoliVersamenti(ecFilter) : new ArrayList<EstrattoConto>(); if (findAll != null && findAll.size() > 0) { numeroZipEntries++; //ordinamento record Collections.sort(findAll, new EstrattoContoComparator()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ZipEntry pagamentoCsv = new ZipEntry("pagamenti.csv"); zout.putNextEntry(pagamentoCsv); printer = new Printer(this.getFormat(), baos); printer.printRecord(CSVUtils.getEstrattoContoCsvHeader()); for (EstrattoConto pagamento : findAll) { printer.printRecord(CSVUtils.getEstrattoContoAsCsvRow(pagamento, this.sdf)); } } finally { try { if (printer != null) { printer.close(); } } catch (Exception e) { throw new Exception("Errore durante la chiusura dello stream ", e); } } zout.write(baos.toByteArray()); zout.closeEntry(); } } if (esportaPdf) { Map<String, Dominio> mappaInputDomini = new HashMap<String, Dominio>(); Map<String, List<Long>> mappaInputEstrattoConto = new HashMap<String, List<Long>>(); SingoliVersamentiBD singoliVersamentiBD = new SingoliVersamentiBD(bd); for (Long idSingoloVersamento : ids) { SingoloVersamento singoloVersamento = singoliVersamentiBD .getSingoloVersamento(idSingoloVersamento); Versamento versamento = singoloVersamento.getVersamento(bd); // Prelevo il dominio UnitaOperativa uo = AnagraficaManager.getUnitaOperativa(bd, versamento.getIdUo()); Dominio dominio = AnagraficaManager.getDominio(bd, uo.getIdDominio()); // Aggrego i versamenti per dominio per generare gli estratti conto List<Long> idSingoliVersamentiDominio = null; if (mappaInputEstrattoConto.containsKey(dominio.getCodDominio())) { idSingoliVersamentiDominio = mappaInputEstrattoConto.get(dominio.getCodDominio()); } else { idSingoliVersamentiDominio = new ArrayList<Long>(); mappaInputEstrattoConto.put(dominio.getCodDominio(), idSingoliVersamentiDominio); mappaInputDomini.put(dominio.getCodDominio(), dominio); } idSingoliVersamentiDominio.add(idSingoloVersamento); } List<it.govpay.core.business.model.EstrattoConto> listInputEstrattoConto = new ArrayList<it.govpay.core.business.model.EstrattoConto>(); for (String codDominio : mappaInputEstrattoConto.keySet()) { it.govpay.core.business.model.EstrattoConto input = it.govpay.core.business.model.EstrattoConto .creaEstrattoContoPagamentiPDF(mappaInputDomini.get(codDominio), mappaInputEstrattoConto.get(codDominio)); listInputEstrattoConto.add(input); } it.govpay.core.business.EstrattoConto estrattoContoBD = new it.govpay.core.business.EstrattoConto( bd); List<it.govpay.core.business.model.EstrattoConto> listOutputEstattoConto = estrattoContoBD .getEstrattoContoPagamenti(listInputEstrattoConto, pathLoghi); for (it.govpay.core.business.model.EstrattoConto estrattoContoOutput : listOutputEstattoConto) { Map<String, ByteArrayOutputStream> estrattoContoVersamenti = estrattoContoOutput.getOutput(); for (String nomeEntry : estrattoContoVersamenti.keySet()) { numeroZipEntries++; ByteArrayOutputStream baos = estrattoContoVersamenti.get(nomeEntry); ZipEntry estrattoContoEntry = new ZipEntry(nomeEntry); // ZipEntry estrattoContoEntry = new ZipEntry(estrattoContoOutput.getDominio().getCodDominio() + "/" + nomeEntry); zout.putNextEntry(estrattoContoEntry); zout.write(baos.toByteArray()); zout.closeEntry(); } } } // se non ho inserito nessuna entry if (numeroZipEntries == 0) { String noEntriesTxt = "/README"; ZipEntry entryTxt = new ZipEntry(noEntriesTxt); zout.putNextEntry(entryTxt); zout.write("Non sono state trovate informazioni sui pagamenti selezionati.".getBytes()); zout.closeEntry(); } zout.flush(); zout.close(); this.log.info("Esecuzione " + methodName + " completata."); return fileName; } catch (WebApplicationException e) { throw e; } catch (ExportException e) { throw e; } catch (Exception e) { this.log.error(e.getMessage(), e); throw new ConsoleException(e); } }