Commit 057ae483 authored by Matija Obreza's avatar Matija Obreza
Browse files

Previews: Generate descriptors when parsing is complete

parent b7ebd000
...@@ -90,7 +90,7 @@ public class PreviewApi { ...@@ -90,7 +90,7 @@ public class PreviewApi {
@ApiOperation(nickname="getPreview", value = "Get the overview of the parsed dataset", notes = "Use the same reference UUID as provided when ingesting a dataset") @ApiOperation(nickname="getPreview", value = "Get the overview of the parsed dataset", notes = "Use the same reference UUID as provided when ingesting a dataset")
public Preview get(@ApiParam(value = "Your reference UUID", required = true) @PathVariable UUID uuid) { public Preview get(@ApiParam(value = "Your reference UUID", required = true) @PathVariable UUID uuid) {
Preview preview = previewService.getPreview(uuid); Preview preview = previewService.getPreview(uuid);
previewService.generateDescriptors(uuid);
if (preview == null) { if (preview == null) {
throw new NoSuchThingException("No preview for reference UUID=" + uuid); throw new NoSuchThingException("No preview for reference UUID=" + uuid);
} }
......
...@@ -44,7 +44,7 @@ public interface PreviewService { ...@@ -44,7 +44,7 @@ public interface PreviewService {
Preview registerSheet(String id, PreviewSheet sheet); Preview registerSheet(String id, PreviewSheet sheet);
void generateDescriptors(UUID referenceUuid); Preview generateDescriptors(UUID referenceUuid);
class StatisticsData implements Serializable { class StatisticsData implements Serializable {
private static final long serialVersionUID = 3826590456962597612L; private static final long serialVersionUID = 3826590456962597612L;
......
...@@ -29,6 +29,7 @@ import java.util.UUID; ...@@ -29,6 +29,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.bson.Document; import org.bson.Document;
...@@ -111,6 +112,9 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean { ...@@ -111,6 +112,9 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean {
Calendar expiry = Calendar.getInstance(); Calendar expiry = Calendar.getInstance();
expiry.add(Calendar.MINUTE, 5); expiry.add(Calendar.MINUTE, 5);
p.setExpires(expiry.getTime()); p.setExpires(expiry.getTime());
} else if (state == State.READY) {
// Generate descriptor data
updateDescriptors(p);
} }
return previewRepository.save(p); return previewRepository.save(p);
} else { } else {
...@@ -197,9 +201,26 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean { ...@@ -197,9 +201,26 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean {
} }
@Override @Override
public void generateDescriptors(UUID referenceUuid) { public Preview generateDescriptors(UUID referenceUuid) {
Preview preview = previewRepository.findByReferenceUuid(referenceUuid); Preview preview = previewRepository.findByReferenceUuid(referenceUuid);
if (preview == null) {
LOG.warn("Cannot generate descriptors, preview for {} not found.", referenceUuid);
return preview;
} else {
return previewRepository.save(updateDescriptors(preview));
}
}
private Preview updateDescriptors(Preview preview) {
assert(preview != null);
List<PreviewSheet> previewSheets = preview.getSheets(); List<PreviewSheet> previewSheets = preview.getSheets();
if (CollectionUtils.isEmpty(previewSheets)) {
LOG.warn("Cannot generate descriptors for id={}, no sheets available.", preview.getId());
return preview;
}
var referenceUuid = preview.getReferenceUuid();
// generate descriptors for all sheets // generate descriptors for all sheets
for (PreviewSheet previewSheet : previewSheets) { for (PreviewSheet previewSheet : previewSheets) {
...@@ -244,7 +265,7 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean { ...@@ -244,7 +265,7 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean {
} }
preview.setSheets(previewSheets); preview.setSheets(previewSheets);
previewRepository.save(preview); return preview;
} }
private boolean checkIsNumericValues(Set<Object> values) { private boolean checkIsNumericValues(Set<Object> values) {
......
...@@ -49,7 +49,6 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -49,7 +49,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opencsv.CSVWriter; import com.opencsv.CSVWriter;
public class PreviewApiTest extends AbstractApiTest { public class PreviewApiTest extends AbstractApiTest {
...@@ -108,7 +107,7 @@ public class PreviewApiTest extends AbstractApiTest { ...@@ -108,7 +107,7 @@ public class PreviewApiTest extends AbstractApiTest {
} }
/** /**
* Ingest exel test. * Ingest Excel test.
*/ */
@Test @Test
public void ingestExelTest() throws Exception { public void ingestExelTest() throws Exception {
...@@ -182,18 +181,18 @@ public class PreviewApiTest extends AbstractApiTest { ...@@ -182,18 +181,18 @@ public class PreviewApiTest extends AbstractApiTest {
// }); // });
/*@formatter:off*/ // /*@formatter:off*/
mockMvc // mockMvc
.perform(post(PreviewApi.CONTROLLER_URL.concat("/{uuid}/generate-descriptors"), uuid.toString()).characterEncoding("UTF8")) // .perform(post(PreviewApi.CONTROLLER_URL.concat("/{uuid}/generate-descriptors"), uuid.toString()).characterEncoding("UTF8"))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print()) //// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk()) // .andExpect(status().isOk())
; // ;
/*@formatter:on*/ // /*@formatter:on*/
preview = previewService.getPreview(uuid); preview = previewService.getPreview(uuid);
PreviewSheet sheet0 = preview.getSheets().get(0); PreviewSheet sheet0 = preview.getSheets().get(0);
assertThat(sheet0.rowCount, is(2)); assertThat(sheet0.rowCount, is(2));
// System.err.println(new ObjectMapper().writeValueAsString(sheet0)); // System.err.println(new com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(sheet0));
assertThat(sheet0.descriptors.get(0).columnName, is("INSTCODE")); assertThat(sheet0.descriptors.get(0).columnName, is("INSTCODE"));
} }
...@@ -250,13 +249,13 @@ public class PreviewApiTest extends AbstractApiTest { ...@@ -250,13 +249,13 @@ public class PreviewApiTest extends AbstractApiTest {
// Waiting for data inserting to complete // Waiting for data inserting to complete
Thread.sleep(500); Thread.sleep(500);
/*@formatter:off*/ // /*@formatter:off*/
mockMvc // mockMvc
.perform(post(PreviewApi.CONTROLLER_URL.concat("/{uuid}/generate-descriptors"), uuid.toString()).characterEncoding("UTF8")) // .perform(post(PreviewApi.CONTROLLER_URL.concat("/{uuid}/generate-descriptors"), uuid.toString()).characterEncoding("UTF8"))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print()) //// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk()) // .andExpect(status().isOk())
; // ;
/*@formatter:on*/ // /*@formatter:on*/
Preview preview = previewService.getPreview(uuid); Preview preview = previewService.getPreview(uuid);
assertThat(preview, is(notNullValue())); assertThat(preview, is(notNullValue()));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment