Search in sources :

Example 1 with VoidWork

use of com.googlecode.objectify.VoidWork in project activityinfo by bedatadriven.

the class FixSubForm method maybeFixForm.

private boolean maybeFixForm(final PrintWriter logger, MySqlQueryExecutor executor, ResourceId parentFormId) {
    logger.println("Fixing " + parentFormId + "...");
    final MySqlStorageProvider catalog = new MySqlStorageProvider(executor);
    FormStorage parentForm = catalog.getForm(parentFormId).get();
    final FormClass formClass = parentForm.getFormClass();
    logger.println("Loaded activity " + parentForm.getFormClass().getLabel());
    final List<FormField> updated = new ArrayList<>();
    ObjectifyService.run(new VoidWork() {

        @Override
        public void vrun() {
            Hrd.ofy().transact(new VoidWork() {

                @Override
                public void vrun() {
                    for (FormField formField : formClass.getFields()) {
                        if (formField.getType() instanceof SubFormReferenceType) {
                            if (maybeFixForm(logger, formClass, formField)) {
                                updated.add(formField);
                            }
                        }
                    }
                }
            });
        }
    });
    logger.println("TX COMPLETED!");
    if (!updated.isEmpty()) {
        logger.println("Updating parent form schema...");
        catalog.createOrUpdateFormSchema(formClass);
        return true;
    } else {
        return false;
    }
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormStorage(org.activityinfo.store.spi.FormStorage) VoidWork(com.googlecode.objectify.VoidWork) FormClass(org.activityinfo.model.form.FormClass) ArrayList(java.util.ArrayList) MySqlStorageProvider(org.activityinfo.store.mysql.MySqlStorageProvider) FormField(org.activityinfo.model.form.FormField)

Example 2 with VoidWork

use of com.googlecode.objectify.VoidWork in project activityinfo by bedatadriven.

the class JobResourceTest method startJob.

@Test
public void startJob() {
    final org.activityinfo.json.JsonParser parser = new org.activityinfo.json.JsonParser();
    final Queue queue = QueueFactory.getDefaultQueue();
    final AuthenticatedUser user = new AuthenticatedUser("XYZ", 1, "akbertram@gmail.com");
    final JobResource resource = new JobResource(Providers.of(user), queue);
    // First request starts the job
    final String jobId = ObjectifyService.run(new Work<String>() {

        @Override
        public String run() {
            TableModel tableModel = ImmutableTableModel.builder().formId(ResourceId.valueOf("FORM1")).build();
            ExportFormJob exportForm = new ExportFormJob(tableModel);
            JobRequest request = new JobRequest(exportForm, "en");
            Response response = resource.start(request.toJsonObject().toJson());
            JsonValue resultObject = parser.parse((String) response.getEntity());
            JobStatus result = JobStatus.fromJson(resultObject);
            assertThat(result.getState(), equalTo(JobState.STARTED));
            return result.getId();
        }
    });
    // Second request retrieves status
    ObjectifyService.run(new VoidWork() {

        @Override
        public void vrun() {
            Response statusResponse = resource.get(jobId);
            JsonValue statusObject = parser.parse(((String) statusResponse.getEntity()));
            JobStatus status = JobStatus.fromJson(statusObject);
            assertThat(status.getState(), equalTo(JobState.STARTED));
        }
    });
}
Also used : JsonValue(org.activityinfo.json.JsonValue) AuthenticatedUser(org.activityinfo.legacy.shared.AuthenticatedUser) ExportFormJob(org.activityinfo.model.job.ExportFormJob) Response(javax.ws.rs.core.Response) JobStatus(org.activityinfo.model.job.JobStatus) JobRequest(org.activityinfo.model.job.JobRequest) VoidWork(com.googlecode.objectify.VoidWork) Queue(com.google.appengine.api.taskqueue.Queue) TableModel(org.activityinfo.model.analysis.TableModel) ImmutableTableModel(org.activityinfo.model.analysis.ImmutableTableModel) Test(org.junit.Test)

Example 3 with VoidWork

use of com.googlecode.objectify.VoidWork in project teammates by TEAMMATES.

the class FeedbackSessionsDb method addInstructorRespondents.

// The objectify library does not support throwing checked exceptions inside transactions
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
public void addInstructorRespondents(List<String> emails, FeedbackSessionAttributes feedbackSession) throws InvalidParametersException, EntityDoesNotExistException {
    Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, emails);
    Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, feedbackSession);
    feedbackSession.sanitizeForSaving();
    if (!feedbackSession.isValid()) {
        throw new InvalidParametersException(feedbackSession.getInvalidityInfo());
    }
    try {
        ofy().transact(new VoidWork() {

            @Override
            public void vrun() {
                FeedbackSession fs = getEntity(feedbackSession);
                if (fs == null) {
                    throw new RuntimeException(new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT + feedbackSession.toString()));
                }
                fs.getRespondingInstructorList().addAll(emails);
                saveEntity(fs, feedbackSession);
            }
        });
    } catch (RuntimeException e) {
        if (e.getCause() instanceof EntityDoesNotExistException) {
            throw (EntityDoesNotExistException) e.getCause();
        }
        throw e;
    }
}
Also used : FeedbackSession(teammates.storage.entity.FeedbackSession) VoidWork(com.googlecode.objectify.VoidWork) InvalidParametersException(teammates.common.exception.InvalidParametersException) EntityDoesNotExistException(teammates.common.exception.EntityDoesNotExistException)

Example 4 with VoidWork

use of com.googlecode.objectify.VoidWork in project teammates by TEAMMATES.

the class FeedbackSessionsDb method deleteStudentRespondent.

// The objectify library does not support throwing checked exceptions inside transactions
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
public void deleteStudentRespondent(String email, FeedbackSessionAttributes feedbackSession) throws EntityDoesNotExistException, InvalidParametersException {
    Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, email);
    Assumption.assertNotNull(Const.StatusCodes.DBLEVEL_NULL_INPUT, feedbackSession);
    feedbackSession.sanitizeForSaving();
    if (!feedbackSession.isValid()) {
        throw new InvalidParametersException(feedbackSession.getInvalidityInfo());
    }
    try {
        ofy().transact(new VoidWork() {

            @Override
            public void vrun() {
                FeedbackSession fs = getEntity(feedbackSession);
                if (fs == null) {
                    throw new RuntimeException(new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT + feedbackSession.toString()));
                }
                fs.getRespondingStudentList().remove(email);
                saveEntity(fs, feedbackSession);
            }
        });
    } catch (RuntimeException e) {
        if (e.getCause() instanceof EntityDoesNotExistException) {
            throw (EntityDoesNotExistException) e.getCause();
        }
        throw e;
    }
}
Also used : FeedbackSession(teammates.storage.entity.FeedbackSession) VoidWork(com.googlecode.objectify.VoidWork) InvalidParametersException(teammates.common.exception.InvalidParametersException) EntityDoesNotExistException(teammates.common.exception.EntityDoesNotExistException)

Example 5 with VoidWork

use of com.googlecode.objectify.VoidWork in project activityinfo by bedatadriven.

the class JobTaskServlet method markFailed.

private void markFailed(final JobEntity jobEntity, Exception e) {
    LOGGER.log(Level.SEVERE, "Job " + jobEntity.getType() + " failed: " + e.getMessage(), e);
    JobStore.ofy().transact(new VoidWork() {

        @Override
        public void vrun() {
            JobEntity updatedEntity = JobStore.getUserJob(JobStore.getWebSafeKeyString(jobEntity)).now();
            if (updatedEntity.getState() != JobState.STARTED) {
                return;
            }
            updatedEntity.setState(JobState.FAILED);
            JobStore.ofy().save().entity(updatedEntity).now();
        }
    });
}
Also used : VoidWork(com.googlecode.objectify.VoidWork)

Aggregations

VoidWork (com.googlecode.objectify.VoidWork)8 EntityDoesNotExistException (teammates.common.exception.EntityDoesNotExistException)4 InvalidParametersException (teammates.common.exception.InvalidParametersException)4 FeedbackSession (teammates.storage.entity.FeedbackSession)4 FormClass (org.activityinfo.model.form.FormClass)2 Test (org.junit.Test)2 Queue (com.google.appengine.api.taskqueue.Queue)1 Optional (com.google.common.base.Optional)1 ArrayList (java.util.ArrayList)1 Response (javax.ws.rs.core.Response)1 JsonValue (org.activityinfo.json.JsonValue)1 AuthenticatedUser (org.activityinfo.legacy.shared.AuthenticatedUser)1 ImmutableTableModel (org.activityinfo.model.analysis.ImmutableTableModel)1 TableModel (org.activityinfo.model.analysis.TableModel)1 FormField (org.activityinfo.model.form.FormField)1 ExportFormJob (org.activityinfo.model.job.ExportFormJob)1 JobRequest (org.activityinfo.model.job.JobRequest)1 JobStatus (org.activityinfo.model.job.JobStatus)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)1