use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class DatabaseResource method createFormFromXForm.
@POST
@Path("/forms")
@Consumes("application/xml")
public Response createFormFromXForm(@Context UriInfo uri, XForm xForm) {
UserDatabaseDTOWithForms schema = getSchema();
LocationTypeDTO locationType = schema.getCountry().getNullLocationType();
ActivityFormDTO activityDTO = new ActivityFormDTO();
activityDTO.setName(xForm.getHead().getTitle());
activityDTO.set("databaseId", databaseId);
activityDTO.set("locationTypeId", locationType.getId());
CreateResult createResult = dispatcher.execute(new CreateEntity(activityDTO));
int activityId = createResult.getNewId();
XFormReader builder = new XFormReader(xForm);
FormClass formClass = builder.build();
formClass.setId(CuidAdapter.activityFormClass(activityId));
formClass.setDatabaseId(CuidAdapter.databaseId(databaseId));
MySqlStorageProvider formCatalog = (MySqlStorageProvider) catalog.get();
formCatalog.createOrUpdateFormSchema(formClass);
return Response.created(uri.getAbsolutePathBuilder().path(RootResource.class).path("forms").path(formClass.getId().asString()).build()).build();
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class XFormSubmissionResource method submit.
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_XML)
public Response submit(byte[] bytes) {
XFormInstance instance = new XFormInstanceImpl(bytes);
AuthenticatedUser user = authenticationTokenService.authenticate(instance.getAuthenticationToken());
FormClass formClass = locator.getFormClass(instance.getFormClassId());
ResourceId formId = newLegacyFormInstanceId(formClass.getId());
FormInstance formInstance = new FormInstance(formId, formClass.getId());
String instanceId = instance.getId();
LOGGER.log(Level.INFO, "Saving XForm " + instance.getId() + " as " + formId);
for (FormField formField : formClass.getFields()) {
if (formField.getType().isUpdatable()) {
Optional<Element> element = instance.getFieldContent(formField.getId());
if (element.isPresent()) {
formInstance.set(formField.getId(), tryParse(formInstance, formField, element.get()));
} else if (isLocation(formClass, formField)) {
FieldType fieldType = formField.getType();
Optional<Element> gpsField = instance.getFieldContent(field(formClass.getId(), GPS_FIELD));
Optional<Element> nameField = instance.getFieldContent(field(formClass.getId(), LOCATION_NAME_FIELD));
if (fieldType instanceof ReferenceType && gpsField.isPresent() && nameField.isPresent()) {
ResourceId locationFieldId = field(formClass.getId(), LOCATION_FIELD);
int newLocationId = new KeyGenerator().generateInt();
ReferenceType locationRefType = (ReferenceType) fieldType;
if (locationRefType.getRange().isEmpty()) {
throw new IllegalStateException("Location field has empty range");
}
ResourceId locationFormId = locationRefType.getRange().iterator().next();
int locationTypeId = getLegacyIdFromCuid(locationFormId);
FieldValue fieldValue = new ReferenceValue(new RecordRef(locationFormId, locationInstanceId(newLocationId)));
String name = OdkHelper.extractText(nameField.get());
if (Strings.isNullOrEmpty(name)) {
throw new WebApplicationException(Response.status(BAD_REQUEST).entity("Name value for location field is blank. ").build());
}
Optional<GeoPoint> geoPoint = parseLocation(gpsField);
formInstance.set(locationFieldId, fieldValue);
createLocation(newLocationId, locationTypeId, name, geoPoint);
}
}
}
}
ensurePartnerIsSet(formClass, formInstance);
if (!instanceIdService.exists(instanceId)) {
for (FieldValue fieldValue : formInstance.getFieldValueMap().values()) {
if (fieldValue instanceof AttachmentValue) {
persist(user, instance, (AttachmentValue) fieldValue);
}
}
locator.persist(formInstance);
instanceIdService.submit(instanceId);
}
// Backup the original XForm in case something went wrong with processing
submissionArchiver.backup(formClass.getId(), formId, ByteSource.wrap(bytes));
return Response.status(CREATED).build();
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class SchemaCsvWriterV3 method writeForms.
@VisibleForTesting
void writeForms(UserDatabaseDTO db, List<ResourceId> formIds) throws IOException {
writeHeaders();
Map<ResourceId, FormClass> formClasses = catalog.getFormClasses(formIds);
for (ResourceId formId : formIds) {
FormClass formClass = formClasses.get(formId);
if (formClass == null) {
throw new IllegalStateException("No FormClass for " + formId);
}
writeForm(db, formClass);
}
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class SchemaCsvWriterV3 method writeSubForm.
private void writeSubForm(FieldContext context, FormField field) throws IOException {
SubFormReferenceType fieldType = (SubFormReferenceType) field.getType();
FormClass subFormClass = catalog.getFormClass(fieldType.getClassId());
FieldContext subFormContext = context.subForm(field, subFormClass);
for (FormField subField : subFormClass.getFields()) {
if (subField.getType() instanceof EnumType) {
writeEnumItems(subFormContext, subField, ((EnumType) subField.getType()).getValues());
} else {
writeField(subFormContext, subField);
}
}
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class CreateOrUpdateRecord method vrun.
@Override
public void vrun() {
FormEntity rootEntity = ofy().load().key(FormEntity.key(formId)).safe();
FormClass formClass = ofy().load().key(FormSchemaEntity.key(formId)).safe().readFormClass();
long currentVersion = rootEntity.getVersion();
long newVersion = currentVersion + 1;
rootEntity.setVersion(newVersion);
FormRecordEntity existingEntity = ofy().load().key(FormRecordEntity.key(formClass, update.getRecordId())).now();
FormRecordEntity updated;
RecordChangeType changeType;
if (existingEntity != null) {
updated = existingEntity;
changeType = update.isDeleted() ? RecordChangeType.DELETED : RecordChangeType.UPDATED;
} else {
updated = new FormRecordEntity(formId, update.getRecordId());
changeType = RecordChangeType.CREATED;
if (update.isDeleted()) {
throw new InvalidUpdateException("Creation of entity with deleted flag is not allowed.");
}
if (formClass.getParentFormId().isPresent()) {
ResourceId parentId = update.getParentId();
if (parentId == null) {
throw new InvalidUpdateException("@parent is required for subform submissions");
}
updated.setParentRecordId(parentId);
}
}
updated.setVersion(newVersion);
updated.setSchemaVersion(rootEntity.getSchemaVersion());
updated.setFieldValues(formClass, update.getChangedFieldValues());
// Store a copy as a snapshot
FormRecordSnapshotEntity snapshotEntity = new FormRecordSnapshotEntity(update.getUserId(), changeType, updated);
if (update.isDeleted()) {
ofy().save().entities(rootEntity, snapshotEntity);
ofy().delete().entities(updated);
} else {
ofy().save().entities(rootEntity, updated, snapshotEntity);
}
}
Aggregations