use of org.openforis.collect.model.CollectSurvey in project collect by openforis.
the class SurveyController method cloneIntoTemporarySurvey.
@Transactional
@RequestMapping(value = "cloneintotemporary/{surveyId}", method = POST)
@ResponseBody
public Response cloneIntoTemporarySurvey(@PathVariable int surveyId) throws Exception {
Response response = new Response();
User loggedUser = sessionManager.getLoggedUser();
CollectSurvey survey = surveyManager.getOrLoadSurveyById(surveyId);
if (survey.isPublished()) {
String surveyUri = survey.getUri();
CollectSurvey temporarySurvey = surveyManager.createTemporarySurveyFromPublished(surveyUri, loggedUser);
response.setObject(temporarySurvey.getId());
} else {
response.setErrorStatus();
response.setErrorMessage(String.format("Survey with id %d is not published as expected", surveyId));
}
return response;
}
use of org.openforis.collect.model.CollectSurvey in project collect by openforis.
the class SurveyController method startExport.
@RequestMapping(value = "export/{id}", method = POST)
@ResponseBody
public JobView startExport(@Valid SurveyExportParameters params, BindingResult result) {
this.surveyBackupJob = null;
String uri = params.getSurveyUri();
// boolean skipValidation = params.isSkipValidation();
SurveySummary surveySummary = surveyManager.loadSummaryByUri(uri);
final CollectSurvey loadedSurvey;
if (surveySummary.isTemporary() && params.getSurveyType() == SurveyType.TEMPORARY) {
loadedSurvey = surveyManager.loadSurvey(surveySummary.getId());
} else {
loadedSurvey = surveyManager.getByUri(uri);
}
switch(params.getOutputFormat()) {
case EARTH:
{
// if (skipValidation || collectEarthSurveyValidator.validate(loadedSurvey).isOk()) {
CollectEarthSurveyExportJob job = jobManager.createJob(CollectEarthSurveyExportJob.class);
job.setSurvey(loadedSurvey);
job.setLanguageCode(params.getLanguageCode());
jobManager.start(job, String.valueOf(loadedSurvey.getId()));
this.surveyBackupJob = job;
return new JobView(job);
}
// break;
case MOBILE:
default:
SurveyBackupJob job = jobManager.createJob(SurveyBackupJob.class);
job.setSurvey(loadedSurvey);
// surveyBackupJob.setIncludeData(parameters.isIncludeData());
// surveyBackupJob.setIncludeRecordFiles(parameters.isIncludeUploadedFiles());
job.setOutputFormat(org.openforis.collect.io.SurveyBackupJob.OutputFormat.valueOf(params.getOutputFormat().name()));
job.setOutputSurveyDefaultLanguage(ObjectUtils.defaultIfNull(params.getLanguageCode(), loadedSurvey.getDefaultLanguage()));
jobManager.start(job, String.valueOf(loadedSurvey.getId()));
this.surveyBackupJob = job;
return new JobView(job);
}
}
use of org.openforis.collect.model.CollectSurvey in project collect by openforis.
the class ValidationController method validateAllRecords.
@RequestMapping(value = "/validateAllRecords.htm", method = RequestMethod.GET)
public void validateAllRecords(HttpServletRequest request, HttpServletResponse response, @RequestParam String s, @RequestParam String r) throws IOException {
final ServletOutputStream outputStream = response.getOutputStream();
try {
if (s == null || r == null) {
outputStream.println("Wrong parameters: please specify 's' (survey) and 'r' (root entity name).");
return;
}
SessionState sessionState = getSessionState(request);
final User user = sessionState.getUser();
final String sessionId = sessionState.getSessionId();
print(outputStream, "Starting validation of all records: ");
final CollectSurvey survey = surveyManager.get(s);
if (survey == null) {
print(outputStream, "Survey not found");
return;
}
RecordFilter filter = new RecordFilter(survey);
filter.setRootEntityId(survey.getSchema().getRootEntityDefinition(r).getId());
final ValidationMessageBuilder validationMessageHelper = ValidationMessageBuilder.createInstance(messageContextHolder);
recordManager.visitSummaries(filter, null, new Visitor<CollectRecordSummary>() {
public void visit(CollectRecordSummary summary) {
try {
String recordKey = validationMessageHelper.getRecordKey(summary);
long start = System.currentTimeMillis();
print(outputStream, "Start validating record: " + recordKey);
Integer id = summary.getId();
Step step = summary.getStep();
recordManager.validateAndSave(survey, user, sessionId, id, step);
long elapsedMillis = System.currentTimeMillis() - start;
print(outputStream, "Validation of record " + recordKey + " completed in " + elapsedMillis + " millis");
} catch (Exception e) {
try {
String message = "ERROR validating record " + summary.getId();
outputStream.println(message);
LOG.error(message);
} catch (IOException e1) {
}
}
}
});
print(outputStream, "End of validation of all records.");
} catch (Exception e) {
outputStream.println("ERROR - Validation of records not completed: " + e.getMessage());
LOG.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
use of org.openforis.collect.model.CollectSurvey in project collect by openforis.
the class DataRestoreController method startRestoreJob.
private DataRestoreJob startRestoreJob(InputStream fileInputStream, boolean newSurvey, String expectedSurveyName, User user, boolean validateRecords, boolean deleteAllRecords, OverwriteStrategy recordOverwriteStrategy) throws IOException, FileNotFoundException, ZipException {
File tempFile = File.createTempFile("ofc_data_restore", ".collect-backup");
FileUtils.copyInputStreamToFile(fileInputStream, tempFile);
SurveyBackupInfo info = extractInfo(tempFile);
CollectSurvey publishedSurvey = findPublishedSurvey(info);
if (newSurvey) {
checkPackagedNewSurveyValidity(info);
} else {
checkPackagedSurveyValidity(info, expectedSurveyName);
}
UserGroup newSurveyUserGroup = userGroupManager.getDefaultPublicUserGroup();
DataRestoreJob job = jobManager.createJob(DataRestoreJob.JOB_NAME, DataRestoreJob.class);
job.setUser(user);
job.setStoreRestoredFile(true);
job.setPublishedSurvey(publishedSurvey);
job.setNewSurveyUserGroup(newSurveyUserGroup);
job.setFile(tempFile);
job.setRecordOverwriteStrategy(recordOverwriteStrategy);
job.setRestoreUploadedFiles(true);
job.setValidateRecords(validateRecords);
job.setDeleteAllRecordsBeforeRestore(deleteAllRecords);
String lockId = extractSurveyUri(tempFile);
jobManager.start(job, lockId);
return job;
}
use of org.openforis.collect.model.CollectSurvey in project collect by openforis.
the class RecordController method newRecord.
@Transactional
@RequestMapping(value = "survey/{surveyId}/data/records", method = POST, consumes = APPLICATION_JSON_VALUE)
@ResponseBody
public RecordProxy newRecord(@PathVariable("surveyId") int surveyId, @RequestBody NewRecordParameters params) throws RecordPersistenceException {
User user = sessionManager.getLoggedUser();
if (user == null) {
user = loadUser(params.getUserId(), params.getUsername());
}
CollectSurvey survey = surveyManager.getById(surveyId);
params.setRootEntityName(ObjectUtils.defaultIfNull(params.getRootEntityName(), survey.getSchema().getFirstRootEntityDefinition().getName()));
params.setVersionName(ObjectUtils.defaultIfNull(params.getVersionName(), survey.getLatestVersion() != null ? survey.getLatestVersion().getName() : null));
params.setUserId(user.getId());
CollectRecord record = recordGenerator.generate(surveyId, params, params.getRecordKey());
return toProxy(record);
}
Aggregations