@RequestMapping(method = RequestMethod.POST) public ModelAndView add(DatasetForm datasetEntryForm, Errors errors, HttpSession session) { ModelAndView mav = new ModelAndView(); validatorDataEntry.validate(datasetEntryForm, errors); if (errors.hasErrors()) { logger.error("Errores en la validacion del formulario"); mav.addObject("privateKey", properties.getReCaptchaPrivateKey()); mav.addObject("publicKey", properties.getReCaptchaPublicKey()); mav.setViewName("dataset/add"); return mav; } PreDataset aDataset = datasetEntryForm.build(); // the resource's length will be calculated later on a separate thread. aDataset.setSize(0); ArgendataUser user = (ArgendataUser) session.getAttribute("user"); if (!aDataset.getDistribution().equals("Download")) { aDataset.setFormat(""); aDataset.setSize(0); } if (!user.isAdmin()) { try { aDataset.setSize(getResourceLength(aDataset)); datasetService.store(aDataset); } catch (Exception e) { logger.error("No se pudo agregar el dataset al store relacional"); logger.error(e.getMessage(), e); mav.setViewName("redirect:../static/error"); return mav; } mav.setViewName("redirect:../user/home?state=1"); } else { Dataset readyDataset = aDataset.toNoRelationalDataset(); try { datasetService.store(readyDataset); } catch (Exception e) { logger.error("No se pudo agregar el dataset al store semántico"); logger.error(e.getMessage(), e); mav.setViewName("redirect:../static/error"); return mav; } mav.setViewName("redirect:../search/searchDataHome"); } asyncResourceLength(aDataset, user.isAdmin()); return mav; }
@Test public void preDatasetStoreTest() { PreDataset dataset = DatasetTestFactory.getPreDataset(); System.out.println(dataset.toString()); datasetService.store(dataset); PreDataset theDataset = datasetService.asyncGetPreDataset(dataset.getTitleId()); System.out.println(theDataset); Assert.assertTrue(dataset.getTitle().equals(theDataset.getTitle())); Assert.assertTrue(dataset.getTitleId().equals(theDataset.getTitleId())); Assert.assertTrue(dataset.getLicense().equals(theDataset.getLicense())); Assert.assertTrue(dataset.getAccessURL().equals(theDataset.getAccessURL())); Assert.assertTrue(dataset.getDataQuality().equals(theDataset.getDataQuality())); Assert.assertTrue(dataset.getLocation().equals(theDataset.getLocation())); Assert.assertTrue(dataset.getModified().equals(theDataset.getModified())); Assert.assertTrue(dataset.getDescription().equals(theDataset.getDescription())); Assert.assertTrue(dataset.getSpatial().equals(theDataset.getSpatial())); Assert.assertTrue(dataset.getTemporal().equals(theDataset.getTemporal())); Assert.assertTrue(dataset.getFormat().equals(theDataset.getFormat())); Assert.assertTrue(dataset.getPublisher().equals(theDataset.getPublisher())); }
private int getResourceLength(PreDataset ds) { // String length = null; int len = 0; String format = null; if (ds.getDistribution() != null && ds.getFormat() != null) { format = ds.getFormat().toLowerCase(); } if (format != null && (format.endsWith("doc") || format.endsWith("docx") || format.endsWith("xls") || format.endsWith("xlsx") || format.endsWith("ppt") || format.endsWith("pptx") || format.endsWith("pps") || format.endsWith("odt") || format.endsWith("ods") || format.endsWith("odp") || format.endsWith("swx") || format.endsWith("sxi") || format.endsWith("wpd") || format.endsWith("pdf") || format.endsWith("rtf") || format.endsWith("txt") || format.endsWith("csv") || format.endsWith("tsv"))) { // me conecto y pido el content-length URL url; try { url = new URL(ds.getAccessURL()); URLConnection conn = url.openConnection(); len = conn.getContentLength(); if (len != -1) { Integer l = (len / 1024); // length= l.toString()+"KB"; len = l; } else { // length = "desconocido"; len = 0; } } catch (MalformedURLException e) { logger.error("No se pudo obtener los headers del recurso " + ds.getAccessURL()); logger.error(e.getMessage(), e); } catch (IOException e) { logger.error("No se pudo obtener los headers del recurso " + ds.getAccessURL()); logger.error(e.getMessage(), e); } } else { // length = "-"; len = 0; } return len; }