use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class LookupKeySet method collectAncestors.
private void collectAncestors(Collection<ResourceId> formIds, Set<ResourceId> set) {
for (ResourceId formId : formIds) {
Collection<ResourceId> parents = parentMap.get(formId);
set.addAll(parents);
collectAncestors(parents, set);
}
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FormInstanceComparatorTest method testList.
private List<FormInstance> testList() {
final ResourceId key1 = ResourceId.valueOf("key1");
final ResourceId key2 = ResourceId.valueOf("key2");
FormInstance instance1 = newInstance(1);
instance1.set(key1, 3);
instance1.set(key2, "d");
FormInstance instance2 = newInstance(2);
instance2.set(key1, 2);
instance2.set(key2, "b");
FormInstance instance3 = newInstance(3);
instance3.set(key1, 1);
instance3.set(key2, "c");
return Lists.newArrayList(instance1, instance2, instance3);
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class RecordRef method fromQualifiedString.
public static RecordRef fromQualifiedString(@Nonnull String string) {
int separatorPos = string.indexOf(SEPARATOR);
if (separatorPos == -1) {
throw new IllegalStateException("Malformed record ref: " + string);
}
ResourceId formId = ResourceId.valueOf(string.substring(0, separatorPos));
ResourceId recordId = ResourceId.valueOf(string.substring(separatorPos + 1));
return new RecordRef(formId, recordId);
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FormResource method getMetadataResponse.
@GET
@NoCache
@Produces(JSON_CONTENT_TYPE)
public FormMetadata getMetadataResponse(@InjectParam DatabaseProviderImpl databaseProvider, @InjectParam AuthenticatedUser user, @QueryParam("localVersion") Long localVersion) {
Optional<FormStorage> storage = backend.getStorage().getForm(formId);
if (!storage.isPresent()) {
return FormMetadata.notFound(formId);
}
ResourceId databaseId = storage.get().getFormClass().getDatabaseId();
UserDatabaseMeta databaseMetadata;
try {
databaseMetadata = databaseProvider.getDatabaseMetadata(databaseId, user.getUserId());
} catch (Exception e) {
// We are initially using this just for locks,
// not actually permissions, so just log the warning for now.
LOGGER.log(Level.SEVERE, "Failed to retrieve metadata for database " + databaseId + " for user " + user.getUserId(), e);
databaseMetadata = new UserDatabaseMeta.Builder().setDatabaseId(databaseId).setLabel("").setOwner(false).build();
}
FormPermissions permissions = backend.getFormSupervisor().getFormPermissions(formId);
if (!permissions.isVisible()) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
} else {
// Workaround for sub form, which we don't yet have indexed to the
// database in which they live.
FormClass schema = storage.get().getFormClass();
RecordLockSet locks;
if (schema.isSubForm()) {
locks = databaseMetadata.getEffectiveLocks(schema.getParentFormId().get());
} else {
locks = databaseMetadata.getEffectiveLocks(formId);
}
return new FormMetadata.Builder().setId(formId).setPermissions(permissions).setSchema(schema).setLocks(locks).setVersion(storage.get().cacheVersion()).build();
}
}
use of org.activityinfo.model.resource.ResourceId 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();
}
Aggregations