Search in sources :

Example 71 with CollectSurvey

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;
}
Also used : Response(org.openforis.commons.web.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) User(org.openforis.collect.model.User) CollectSurvey(org.openforis.collect.model.CollectSurvey) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 72 with CollectSurvey

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);
    }
}
Also used : CollectEarthSurveyExportJob(org.openforis.collect.manager.CollectEarthSurveyExportJob) SurveySummary(org.openforis.collect.model.SurveySummary) JobView(org.openforis.collect.web.controller.CollectJobController.JobView) SurveyBackupJob(org.openforis.collect.io.SurveyBackupJob) CollectSurvey(org.openforis.collect.model.CollectSurvey) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 73 with CollectSurvey

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);
    }
}
Also used : ValidationMessageBuilder(org.openforis.collect.model.validation.ValidationMessageBuilder) SessionState(org.openforis.collect.web.session.SessionState) User(org.openforis.collect.model.User) ServletOutputStream(javax.servlet.ServletOutputStream) Step(org.openforis.collect.model.CollectRecord.Step) IOException(java.io.IOException) IOException(java.io.IOException) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) CollectSurvey(org.openforis.collect.model.CollectSurvey) RecordFilter(org.openforis.collect.model.RecordFilter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 74 with CollectSurvey

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;
}
Also used : SurveyBackupInfo(org.openforis.collect.io.SurveyBackupInfo) CollectSurvey(org.openforis.collect.model.CollectSurvey) DataRestoreJob(org.openforis.collect.io.data.DataRestoreJob) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) UserGroup(org.openforis.collect.model.UserGroup)

Example 75 with CollectSurvey

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);
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) User(org.openforis.collect.model.User) CollectSurvey(org.openforis.collect.model.CollectSurvey) Transactional(org.springframework.transaction.annotation.Transactional) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

CollectSurvey (org.openforis.collect.model.CollectSurvey)329 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)53 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)40 UIOptions (org.openforis.collect.metamodel.ui.UIOptions)38 CollectRecord (org.openforis.collect.model.CollectRecord)30 RecordFilter (org.openforis.collect.model.RecordFilter)27 Transactional (org.springframework.transaction.annotation.Transactional)26 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)25 ArrayList (java.util.ArrayList)23 File (java.io.File)21 CollectAnnotations (org.openforis.collect.metamodel.CollectAnnotations)21 Secured (org.springframework.security.access.annotation.Secured)20 User (org.openforis.collect.model.User)19 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)19 SessionState (org.openforis.collect.web.session.SessionState)18 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)17 Test (org.junit.Test)16 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)15 CodeList (org.openforis.idm.metamodel.CodeList)15 Schema (org.openforis.idm.metamodel.Schema)15