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 {
@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) {
Preview preview = previewService.getPreview(uuid);
previewService.generateDescriptors(uuid);
if (preview == null) {
throw new NoSuchThingException("No preview for reference UUID=" + uuid);
}
......
......@@ -44,7 +44,7 @@ public interface PreviewService {
Preview registerSheet(String id, PreviewSheet sheet);
void generateDescriptors(UUID referenceUuid);
Preview generateDescriptors(UUID referenceUuid);
class StatisticsData implements Serializable {
private static final long serialVersionUID = 3826590456962597612L;
......
......@@ -29,6 +29,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.bson.Document;
......@@ -111,6 +112,9 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean {
Calendar expiry = Calendar.getInstance();
expiry.add(Calendar.MINUTE, 5);
p.setExpires(expiry.getTime());
} else if (state == State.READY) {
// Generate descriptor data
updateDescriptors(p);
}
return previewRepository.save(p);
} else {
......@@ -197,9 +201,26 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean {
}
@Override
public void generateDescriptors(UUID referenceUuid) {
public Preview generateDescriptors(UUID 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();
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
for (PreviewSheet previewSheet : previewSheets) {
......@@ -244,7 +265,7 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean {
}
preview.setSheets(previewSheets);
previewRepository.save(preview);
return preview;
}
private boolean checkIsNumericValues(Set<Object> values) {
......
......@@ -49,7 +49,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opencsv.CSVWriter;
public class PreviewApiTest extends AbstractApiTest {
......@@ -108,7 +107,7 @@ public class PreviewApiTest extends AbstractApiTest {
}
/**
* Ingest exel test.
* Ingest Excel test.
*/
@Test
public void ingestExelTest() throws Exception {
......@@ -182,18 +181,18 @@ public class PreviewApiTest extends AbstractApiTest {
// });
/*@formatter:off*/
mockMvc
.perform(post(PreviewApi.CONTROLLER_URL.concat("/{uuid}/generate-descriptors"), uuid.toString()).characterEncoding("UTF8"))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
;
/*@formatter:on*/
// /*@formatter:off*/
// mockMvc
// .perform(post(PreviewApi.CONTROLLER_URL.concat("/{uuid}/generate-descriptors"), uuid.toString()).characterEncoding("UTF8"))
//// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
// .andExpect(status().isOk())
// ;
// /*@formatter:on*/
preview = previewService.getPreview(uuid);
PreviewSheet sheet0 = preview.getSheets().get(0);
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"));
}
......@@ -250,13 +249,13 @@ public class PreviewApiTest extends AbstractApiTest {
// Waiting for data inserting to complete
Thread.sleep(500);
/*@formatter:off*/
mockMvc
.perform(post(PreviewApi.CONTROLLER_URL.concat("/{uuid}/generate-descriptors"), uuid.toString()).characterEncoding("UTF8"))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
;
/*@formatter:on*/
// /*@formatter:off*/
// mockMvc
// .perform(post(PreviewApi.CONTROLLER_URL.concat("/{uuid}/generate-descriptors"), uuid.toString()).characterEncoding("UTF8"))
//// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
// .andExpect(status().isOk())
// ;
// /*@formatter:on*/
Preview preview = previewService.getPreview(uuid);
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