Search in sources :

Example 1 with DataRestoreJob

use of org.openforis.collect.io.data.DataRestoreJob in project collect by openforis.

the class RecordController method startRecordImport.

@RequestMapping(value = "survey/{surveyId}/data/import/records", method = POST)
@ResponseBody
public JobView startRecordImport(@PathVariable("surveyId") int surveyId, @RequestParam List<Integer> entryIdsToImport, @RequestParam(defaultValue = "true") boolean validateRecords) throws IOException {
    RecordProvider recordProvider = dataRestoreSummaryJob.getRecordProvider();
    recordProvider.setConfiguration(new RecordProviderConfiguration(true));
    DataRestoreJob job = jobManager.createJob(TransactionalDataRestoreJob.class);
    job.setFile(dataRestoreSummaryJob.getFile());
    job.setUser(sessionManager.getLoggedUser());
    job.setValidateRecords(validateRecords);
    job.setRecordProvider(recordProvider);
    job.setPackagedSurvey(dataRestoreSummaryJob.getPackagedSurvey());
    job.setPublishedSurvey(dataRestoreSummaryJob.getPublishedSurvey());
    job.setEntryIdsToImport(entryIdsToImport);
    job.setRecordFilesToBeDeleted(dataRestoreSummaryJob.getSummary().getConflictingRecordFiles(entryIdsToImport));
    job.setRestoreUploadedFiles(true);
    job.setValidateRecords(validateRecords);
    jobManager.start(job);
    return new JobView(job);
}
Also used : RecordProvider(org.openforis.collect.io.data.RecordProvider) JobView(org.openforis.collect.web.controller.CollectJobController.JobView) RecordProviderConfiguration(org.openforis.collect.io.data.RecordProviderConfiguration) DataRestoreJob(org.openforis.collect.io.data.DataRestoreJob) TransactionalDataRestoreJob(org.openforis.collect.io.data.TransactionalDataRestoreJob) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with DataRestoreJob

use of org.openforis.collect.io.data.DataRestoreJob in project collect by openforis.

the class DataRestoreController method getRestoreDataRemotelyStatus.

@Secured({ UserRoles.ENTRY })
@RequestMapping(value = "/surveys/data/restorejobs/{jobId}/status.json", method = GET)
@ResponseBody
public RemoteDataRestoreResponse getRestoreDataRemotelyStatus(@PathVariable String jobId) throws IOException {
    RemoteDataRestoreResponse response;
    Job job = jobManager.getJob(jobId);
    if (job == null || !(job instanceof DataRestoreJob)) {
        response = new RemoteDataRestoreResponse();
        response.setErrorStatus();
        response.setErrorMessage("Job not found");
    } else {
        response = createRemoteDataRestoreResponse(job);
    }
    return response;
}
Also used : DataRestoreJob(org.openforis.collect.io.data.DataRestoreJob) Job(org.openforis.concurrency.Job) DataRestoreJob(org.openforis.collect.io.data.DataRestoreJob) Secured(org.springframework.security.access.annotation.Secured) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with DataRestoreJob

use of org.openforis.collect.io.data.DataRestoreJob 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 4 with DataRestoreJob

use of org.openforis.collect.io.data.DataRestoreJob in project collect by openforis.

the class DataRestoreController method abortRestoreDataRemotelyJob.

@RequestMapping(value = "/surveys/data/restore/jobs/{jobId}/abort.json", method = GET)
@ResponseBody
public RemoteDataRestoreResponse abortRestoreDataRemotelyJob(@PathVariable String jobId) throws IOException {
    RemoteDataRestoreResponse response;
    Job job = jobManager.getJob(jobId);
    if (job == null || !(job instanceof DataRestoreJob)) {
        response = new RemoteDataRestoreResponse();
        response.setErrorStatus();
        response.setErrorMessage("Job not found");
    } else {
        job.abort();
        response = createRemoteDataRestoreResponse(job);
    }
    return response;
}
Also used : DataRestoreJob(org.openforis.collect.io.data.DataRestoreJob) Job(org.openforis.concurrency.Job) DataRestoreJob(org.openforis.collect.io.data.DataRestoreJob) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with DataRestoreJob

use of org.openforis.collect.io.data.DataRestoreJob in project collect by openforis.

the class DataRestoreController method restoreDataRemotely.

@RequestMapping(value = "/surveys/{surveyName}/data/restoreremotely.json", method = POST)
@ResponseBody
public RemoteDataRestoreResponse restoreDataRemotely(UploadItem uploadItem, @PathVariable String surveyName, @RequestParam String restoreKey) {
    RemoteDataRestoreResponse response = new RemoteDataRestoreResponse();
    String allowedRestoreKey = configurationManager.getConfiguration().get(ConfigurationItem.ALLOWED_RESTORE_KEY);
    if (StringUtils.isBlank(allowedRestoreKey) || allowedRestoreKey.equals(restoreKey)) {
        try {
            User user = userManager.loadAdminUser();
            boolean newSurvey = surveyManager.get(surveyName) == null;
            DataRestoreJob job = startRestoreJob(uploadItem.getFileData().getInputStream(), newSurvey, surveyName, user, true, false, OverwriteStrategy.OVERWRITE_OLDER);
            response.setJobId(job.getId().toString());
        } catch (Exception e) {
            response.setErrorStatus();
            response.setErrorMessage(e.getMessage());
        }
    } else {
        response.setErrorStatus();
        response.setErrorMessage("Restore not allowed: invalid restore key");
    }
    return response;
}
Also used : User(org.openforis.collect.model.User) DataRestoreJob(org.openforis.collect.io.data.DataRestoreJob) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

DataRestoreJob (org.openforis.collect.io.data.DataRestoreJob)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 TransactionalDataRestoreJob (org.openforis.collect.io.data.TransactionalDataRestoreJob)2 CollectSurvey (org.openforis.collect.model.CollectSurvey)2 Job (org.openforis.concurrency.Job)2 Secured (org.springframework.security.access.annotation.Secured)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ZipException (java.util.zip.ZipException)1 SurveyBackupInfo (org.openforis.collect.io.SurveyBackupInfo)1 RecordProvider (org.openforis.collect.io.data.RecordProvider)1 RecordProviderConfiguration (org.openforis.collect.io.data.RecordProviderConfiguration)1 User (org.openforis.collect.model.User)1 UserGroup (org.openforis.collect.model.UserGroup)1 JobView (org.openforis.collect.web.controller.CollectJobController.JobView)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1