Example usage for org.apache.commons.logging Log error

List of usage examples for org.apache.commons.logging Log error

Introduction

In this page you can find the example usage for org.apache.commons.logging Log error.

Prototype

void error(Object message);

Source Link

Document

Logs a message with error log level.

Usage

From source file:org.opendatakit.aggregate.query.submission.QueryByUIFilterGroup.java

/**
 * Silently skip the submissions that are not retrievable due to malformations
 * of some kind.//from  w  w  w  . j  a  v a2 s .co  m
 * 
 */
public List<Submission> getResultSubmissions(CallingContext cc) throws ODKDatastoreException {

    List<Submission> retrievedSubmissions = new ArrayList<Submission>();

    // retrieve submissions
    QueryResult results = getQueryResult(cursor, fetchLimit);
    List<? extends CommonFieldsBase> submissionEntities = results.getResultList();

    // create a row for each submission
    for (int count = 0; count < submissionEntities.size(); count++) {
        CommonFieldsBase subEntity = submissionEntities.get(count);
        try {
            Submission sub = new Submission((TopLevelDynamicBase) subEntity, getForm(), cc);
            retrievedSubmissions.add(sub);
        } catch (ODKDatastoreException e) {
            Log logger = LogFactory.getLog(QueryByUIFilterGroup.class);
            e.printStackTrace();
            logger.error("Unable to reconstruct submission for " + subEntity.getSchemaName() + "."
                    + subEntity.getTableName() + " uri " + subEntity.getUri());

            if ((e instanceof ODKEntityNotFoundException) || (e instanceof ODKEnumeratedElementException)) {
                // see if we should throw an error or skip processing...
                Boolean skip = ServerPreferencesProperties.getSkipMalformedSubmissions(cc);
                if (skip) {
                    continue;
                } else {
                    throw e;
                }
            } else {
                throw e;
            }
        }
    }

    // advance cursor...
    cursor = results.getResumeCursor();
    return retrievedSubmissions;
}

From source file:org.opendatakit.aggregate.query.submission.QueryByUIFilterGroup.java

public void populateSubmissions(SubmissionUISummary summary, List<FormElementModel> filteredElements,
        ElementFormatter elemFormatter, List<FormElementNamespace> elementTypes, CallingContext cc)
        throws ODKDatastoreException {

    // retrieve submissions
    QueryResult results = getQueryResult(cursor, fetchLimit);
    FormElementModel fem = getForm().getTopLevelGroupElement();

    QueryResumePoint startCursor = results.getStartCursor();
    QueryResumePoint resumeCursor = results.getResumeCursor();
    QueryResumePoint backwardCursor = results.getBackwardCursor();

    // The SubmissionUISummary holds data as presented to the
    // UI layer.  Therefore, if we are paging backward, we need
    // to invert the sense of the query results.
    ///*from www  .  jav a  2s  .  co m*/
    // Determine whether we are going forward or backward...
    boolean isForwardCursor = true;
    if (startCursor != null) {
        isForwardCursor = startCursor.isForwardCursor();
    }

    if (isForwardCursor) {
        // everything matches across the UI and query layer...
        summary.setHasPriorResults(results.hasPriorResults());
        summary.setHasMoreResults(results.hasMoreResults());
        if (startCursor != null) {
            summary.setStartCursor(startCursor.transform());
        } else {
            summary.setStartCursor(null);
        }

        if (resumeCursor != null) {
            summary.setResumeCursor(resumeCursor.transform());
        } else {
            summary.setResumeCursor(null);
        }

        if (backwardCursor != null) {
            summary.setBackwardCursor(backwardCursor.transform());
        } else {
            summary.setBackwardCursor(null);
        }
    } else {
        // we are moving backward; the UI is inverted w.r.t. query.

        // SubmissionUISummary.hasPriorResults is query hasMoreResults
        // SubmissionUISummary.hasMoreResults is query hasPriorResults
        summary.setHasPriorResults(results.hasMoreResults());
        summary.setHasMoreResults(results.hasPriorResults());

        // SubmissionUISummary.startCursor is unchanged
        if (startCursor != null) {
            summary.setStartCursor(startCursor.transform());
        } else {
            summary.setStartCursor(null);
        }

        // SubmissionUISummary.resumeCursor is query backwardCursor
        if (backwardCursor != null) {
            summary.setResumeCursor(backwardCursor.transform());
        } else {
            summary.setResumeCursor(null);
        }

        // SubmissionUISummary.backwardCursor is query resumeCursor
        if (resumeCursor != null) {
            summary.setBackwardCursor(resumeCursor.transform());
        } else {
            summary.setBackwardCursor(null);
        }

    }

    List<SubmissionUI> submissionList = new ArrayList<SubmissionUI>();

    // create a row for each submission
    for (CommonFieldsBase subEntity : results.getResultList()) {
        try {
            Submission sub = new Submission((TopLevelDynamicBase) subEntity, getForm(), cc);
            Row row = sub.getFormattedValuesAsRow(elementTypes, filteredElements, elemFormatter, false, cc);

            SubmissionKey subKey = sub.constructSubmissionKey(fem);
            submissionList.add(new SubmissionUI(row.getFormattedValues(), subKey.toString()));
        } catch (ODKDatastoreException e) {
            Log logger = LogFactory.getLog(QueryByUIFilterGroup.class);
            e.printStackTrace();
            logger.error("Unable to reconstruct submission for " + subEntity.getSchemaName() + "."
                    + subEntity.getTableName() + " uri " + subEntity.getUri());

            if ((e instanceof ODKEntityNotFoundException) || (e instanceof ODKEnumeratedElementException)) {
                // see if we should throw an error or skip processing...
                Boolean skip = ServerPreferencesProperties.getSkipMalformedSubmissions(cc);
                if (skip) {
                    continue;
                } else {
                    throw e;
                }
            } else {
                throw e;
            }
        }
    }
    if (!isForwardCursor) {
        // query has the results in the reverse order.
        // invert them to get them properly ordered.
        Collections.reverse(submissionList);
    }
    summary.getSubmissions().addAll(submissionList);
}

From source file:org.opendatakit.aggregate.submission.Submission.java

public static final Submission fetchSubmission(List<SubmissionKeyPart> parts, CallingContext cc)
        throws ODKFormNotFoundException, ODKDatastoreException {
    if (parts == null || parts.size() == 0) {
        throw new IllegalArgumentException("submission key is empty");
    }// w  ww.  j  a  va2  s  .  c o m
    IForm form = FormFactory.retrieveFormByFormId(parts.get(0).getElementName(), cc);
    if (!form.hasValidFormDefinition()) {
        throw new IllegalArgumentException("Form definition is ill-formed"); // ill-formed
                                                                             // definition
    }

    if (parts.size() < 2) {
        throw new IllegalArgumentException("submission key does not have a top level group");
    }
    SubmissionKeyPart tlg = parts.get(1);
    if (!form.getTopLevelGroupElement().getElementName().equals(tlg.getElementName())) {
        throw new IllegalArgumentException("top level group name: " + tlg.getElementName()
                + " is not as expected: " + form.getTopLevelGroupElement().getElementName());
    }
    if (tlg.getAuri() == null) {
        throw new IllegalArgumentException("submission key does not have top level auri");
    }

    Datastore ds = cc.getDatastore();
    User user = cc.getCurrentUser();
    TopLevelDynamicBase tle = (TopLevelDynamicBase) ds.getEntity(
            form.getTopLevelGroupElement().getFormDataModel().getBackingObjectPrototype(), tlg.getAuri(), user);

    try {
        return new Submission(tle, form, cc);
    } catch (ODKDatastoreException e) {
        Log logger = LogFactory.getLog(Submission.class);
        e.printStackTrace();
        logger.error("Unable to reconstruct submission for " + tle.getSchemaName() + "." + tle.getTableName()
                + " uri " + tle.getUri());

        if ((e instanceof ODKEntityNotFoundException) || (e instanceof ODKEnumeratedElementException)) {
            // see if we should throw an error or skip processing...
            Boolean skip = ServerPreferencesProperties.getSkipMalformedSubmissions(cc);
            if (skip) {
                return null;
            } else {
                throw e;
            }
        } else {
            throw e;
        }
    }

}

From source file:org.opendatakit.aggregate.task.WorksheetCreatorWorkerImpl.java

public final void worksheetCreator() {

    Log logger = LogFactory.getLog(WorksheetCreatorWorkerImpl.class);
    logger.info("Beginning Worksheet Creator: " + miscTasksKey.toString() + " form " + form.getFormId());

    MiscTasks t;/*from w w  w. j  a v a  2s.  co  m*/
    try {
        t = new MiscTasks(miscTasksKey, cc);
    } catch (Exception e) {
        logger.error("worksheetCreator: " + miscTasksKey.toString() + " form " + form.getFormId()
                + " MiscTasks retrieval exception: " + e.toString());
        return;
    }
    // gain lock on the formId itself...
    // the locked resource should be the formId, but for testing
    // it is useful to have the external services collide using
    // formId.  Prefix with MT: to indicate that it is a miscellaneousTask
    // lock.
    Datastore ds = cc.getDatastore();
    User user = cc.getCurrentUser();
    String lockedResourceName = t.getMiscTaskLockName();
    TaskLock formIdTaskLock = ds.createTaskLock(user);

    boolean locked = false;
    try {
        if (formIdTaskLock.obtainLock(pFormIdLockId, lockedResourceName, TaskLockType.WORKSHEET_CREATION)) {
            locked = true;
        }
        formIdTaskLock = null;
    } catch (ODKTaskLockException e1) {
        e1.printStackTrace(); // Occasionally expected...
    }

    if (!locked) {
        return;
    }

    try {
        if (t.getRequestDate().before(form.getCreationDate())) {
            // form is newer, so the task must not refer to this form definition...
            doMarkAsComplete(t);
        } else {
            // worksheet creation request should have been created after the form...
            doWorksheetCreator(logger);
        }
    } catch (Exception e2) {
        // some other unexpected exception...
        logger.error("worksheetCreator: " + miscTasksKey.toString() + " form " + form.getFormId()
                + " Unexpected exception from work body: " + e2.toString());
        e2.printStackTrace();
    } finally {
        formIdTaskLock = ds.createTaskLock(user);
        try {
            for (int i = 0; i < 10; i++) {
                if (formIdTaskLock.releaseLock(pFormIdLockId, lockedResourceName,
                        TaskLockType.WORKSHEET_CREATION))
                    break;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // just move on, this retry mechanism
                    // is to make things nice
                }
            }
        } catch (ODKTaskLockException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.opendatakit.aggregate.task.WorksheetCreatorWorkerImpl.java

public final void doWorksheetCreator(Log logger) {
    try {//www  . j a  v  a2s.  c o  m
        // get spreadsheet
        GoogleSpreadsheet spreadsheet = getGoogleSpreadsheetWithName();

        // verify form has a spreadsheet element
        if (spreadsheet == null) {
            throw new ODKExternalServiceException("unable to find spreadsheet");
        }

        // generate worksheets
        try {
            spreadsheet.generateWorksheets(cc);
            logger.info("doWorksheetCreator: " + miscTasksKey.toString() + " form " + form.getFormId()
                    + " Successful worksheet creation!");
        } catch (AuthenticationException e) {
            logger.error("doWorksheetCreator: " + miscTasksKey.toString() + " form " + form.getFormId()
                    + " Exception: " + e.toString());
            throw new ODKExternalServiceCredentialsException(e);
        } catch (ODKExternalServiceException e) {
            logger.error("doWorksheetCreator: " + miscTasksKey.toString() + " form " + form.getFormId()
                    + " Exception: " + e.toString());
            throw e;
        } catch (Exception e) {
            logger.error("doWorksheetCreator: " + miscTasksKey.toString() + " form " + form.getFormId()
                    + " Exception: " + e.toString());
            throw new ODKExternalServiceException(e);
        }

        // the above may have taken a while -- re-fetch the data to see if it has changed...
        MiscTasks r = new MiscTasks(miscTasksKey, cc);
        if (attemptCount.equals(r.getAttemptCount())) {
            // still the same attempt...
            // if we need to upload submissions, start a task to do so
            UploadSubmissions us = (UploadSubmissions) cc.getBean(BeanDefs.UPLOAD_TASK_BEAN);
            if (!esType.equals(ExternalServicePublicationOption.STREAM_ONLY)) {
                us.createFormUploadTask(spreadsheet.getFormServiceCursor(), true, cc);
            }

            doMarkAsComplete(r);
        }
    } catch (Exception e) {
        logger.error("doWorksheetCreator: " + miscTasksKey.toString() + " form " + form.getFormId()
                + " Initiating failure recovery: " + e.toString());
        failureRecovery(e);
    }
}

From source file:org.opendatakit.aggregate.task.WorksheetCreatorWorkerImpl.java

private void failureRecovery(Exception e) {
    // three exceptions possible:
    // ODKFormNotFoundException, ODKDatastoreException, ODKExternalServiceException, Exception
    e.printStackTrace();/*from   ww w .  j  a v a  2  s  .c o m*/
    MiscTasks r;
    try {
        r = new MiscTasks(miscTasksKey, cc);
        if (attemptCount.equals(r.getAttemptCount())) {
            r.setStatus(FormActionStatus.FAILED);
            r.persist(cc);
        }
    } catch (Exception ex) {
        Log logger = LogFactory.getLog(WorksheetCreatorWorkerImpl.class);
        logger.error("failureRecovery: " + miscTasksKey.toString() + " form " + form.getFormId()
                + " Exception during failure recovery: " + ex.toString());
        // something is hosed -- don't attempt to continue.
        // TODO: watchdog: find this once lastRetryDate is way late?
    }
}

From source file:org.opendatakit.api.odktables.FileManifestService.java

/**
 *
 * @param httpHeaders//from   ww w  . j  av  a2  s.c  o  m
 * @param odkClientVersion
 * @return {@link OdkTablesFileManifest} of all the files meeting the filter criteria.
 * @throws ODKOverQuotaException
 * @throws ODKEntityNotFoundException
 * @throws ODKTaskLockException
 * @throws ODKDatastoreException
 * @throws PermissionDeniedException
 */
@GET
@ApiOperation(value = "Returns a list of application-level files.", response = OdkTablesFileManifest.class)
@Path("{odkClientVersion}")
@Produces({ MediaType.APPLICATION_JSON, ApiConstants.MEDIA_TEXT_XML_UTF8,
        ApiConstants.MEDIA_APPLICATION_XML_UTF8 })
public Response /* OdkTablesFileManifest */ getAppLevelFileManifest(@Context HttpHeaders httpHeaders,
        @PathParam("odkClientVersion") String odkClientVersion) throws ODKEntityNotFoundException,
        ODKOverQuotaException, PermissionDeniedException, ODKDatastoreException, ODKTaskLockException {

    FileManifestManager manifestManager = new FileManifestManager(appId, odkClientVersion, cc);
    OdkTablesFileManifest manifest = null;
    Log log = LogFactory.getLog(FileManifestService.class);

    // retrieve the incoming if-none-match eTag...
    List<String> eTags = httpHeaders.getRequestHeader(HttpHeaders.IF_NONE_MATCH);
    String eTag = (eTags == null || eTags.isEmpty()) ? null : eTags.get(0);
    DbManifestETagEntity eTagEntity = null;
    try {
        try {
            eTagEntity = DbManifestETags.getTableIdEntry(DbManifestETags.APP_LEVEL, cc);
        } catch (ODKEntityNotFoundException e) {
            // ignore...
        }
        if (eTag != null && eTagEntity != null && eTag.equals(eTagEntity.getManifestETag())) {
            log.info("The etag about to be returned for Not Modified is " + eTag);
            return Response.status(Status.NOT_MODIFIED).header(HttpHeaders.ETAG, eTag)
                    .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                    .header("Access-Control-Allow-Origin", "*")
                    .header("Access-Control-Allow-Credentials", "true").build();
        }
        // we want just the app-level files.
        manifest = manifestManager.getManifestForAppLevelFiles();

    } catch (ODKDatastoreException e) {
        log.error("Datastore exception in getting the file manifest");
        e.printStackTrace();
    }
    if (manifest == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unable to retrieve manifest.")
                .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                .header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Credentials", "true")
                .build();
    } else {
        String newETag = Integer.toHexString(manifest.hashCode());
        // create a new eTagEntity if there isn't one already...
        if (eTagEntity == null) {
            eTagEntity = DbManifestETags.createNewEntity(DbManifestETags.APP_LEVEL, cc);
            eTagEntity.setManifestETag(newETag);
            eTagEntity.put(cc);
        } else if (!newETag.equals(eTagEntity.getManifestETag())) {
            log.error("App-level Manifest ETag does not match computed value!");
            eTagEntity.setManifestETag(newETag);
            eTagEntity.put(cc);
        }
        // and whatever the eTag is in that entity is the eTag we should return...
        eTag = eTagEntity.getManifestETag();
        UriBuilder uriBuilder = info.getBaseUriBuilder();
        uriBuilder.path(OdkTables.class);
        //UriBuilder uriBuilder = UriBuilder.fromResource(OdkTables.class);
        uriBuilder.path(OdkTables.class, "getFilesService");
        // now supply the downloadUrl...
        for (OdkTablesFileManifestEntry entry : manifest.getFiles()) {
            URI self = uriBuilder.clone().path(FileService.class, "getFile")
                    .build(ArrayUtils.toArray(appId, odkClientVersion, entry.filename), false);
            try {
                entry.downloadUrl = self.toURL().toExternalForm();
            } catch (MalformedURLException e) {
                e.printStackTrace();
                throw new IllegalArgumentException("Unable to convert to URL");
            }
        }

        log.info("The etag about to be returned for OK is " + eTag);
        return Response.ok(manifest).header(HttpHeaders.ETAG, eTag)
                .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                .header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Credentials", "true")
                .build();
    }
}

From source file:org.opendatakit.api.odktables.FileManifestService.java

/**
 *
 * @param httpHeaders/*from   ww  w.  jav  a  2 s. c  o  m*/
 * @param odkClientVersion
 * @param tableId
 * @return {@link OdkTablesFileManifest} of all the files meeting the filter criteria.
 * @throws ODKOverQuotaException
 * @throws ODKEntityNotFoundException
 * @throws ODKTaskLockException
 * @throws ODKDatastoreException
 * @throws PermissionDeniedException
 */
@GET
@ApiOperation(value = "Returns a the list of files which make up a form definition.", response = OdkTablesFileManifest.class)
@Path("{odkClientVersion}/{tableId}")
@Produces({ MediaType.APPLICATION_JSON, ApiConstants.MEDIA_TEXT_XML_UTF8,
        ApiConstants.MEDIA_APPLICATION_XML_UTF8 })
public Response /* OdkTablesFileManifest */ getTableIdFileManifest(@Context HttpHeaders httpHeaders,
        @PathParam("odkClientVersion") String odkClientVersion, @PathParam("tableId") String tableId)
        throws ODKEntityNotFoundException, ODKOverQuotaException, PermissionDeniedException,
        ODKDatastoreException, ODKTaskLockException {

    FileManifestManager manifestManager = new FileManifestManager(appId, odkClientVersion, cc);
    OdkTablesFileManifest manifest = null;

    // retrieve the incoming if-none-match eTag...
    List<String> eTags = httpHeaders.getRequestHeader(HttpHeaders.IF_NONE_MATCH);
    String eTag = (eTags == null || eTags.isEmpty()) ? null : eTags.get(0);
    DbManifestETagEntity eTagEntity = null;
    try {
        try {
            eTagEntity = DbManifestETags.getTableIdEntry(tableId, cc);
        } catch (ODKEntityNotFoundException e) {
            // ignore...
        }
        if (eTag != null && eTagEntity != null && eTag.equals(eTagEntity.getManifestETag())) {
            return Response.status(Status.NOT_MODIFIED).header(HttpHeaders.ETAG, eTag)
                    .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                    .header("Access-Control-Allow-Origin", "*")
                    .header("Access-Control-Allow-Credentials", "true").build();
        }
        // we want just the files for the table.
        manifest = manifestManager.getManifestForTable(tableId);
    } catch (ODKDatastoreException e) {
        Log log = LogFactory.getLog(FileManifestService.class);
        log.error("Datastore exception in getting the file manifest");
        e.printStackTrace();
    }
    if (manifest == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unable to retrieve manifest.")
                .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                .header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Credentials", "true")
                .build();
    } else {
        String newETag = Integer.toHexString(manifest.hashCode());
        // create a new eTagEntity if there isn't one already...
        if (eTagEntity == null) {
            eTagEntity = DbManifestETags.createNewEntity(tableId, cc);
            eTagEntity.setManifestETag(newETag);
            eTagEntity.put(cc);
        } else if (!newETag.equals(eTagEntity.getManifestETag())) {
            Log log = LogFactory.getLog(FileManifestService.class);
            log.error("Table-level (" + tableId + ") Manifest ETag does not match computed value!");
            eTagEntity.setManifestETag(newETag);
            eTagEntity.put(cc);
        }
        // and whatever the eTag is in that entity is the eTag we should return...
        eTag = eTagEntity.getManifestETag();
        fixDownloadUrls(info, appId, odkClientVersion, manifest);

        return Response.ok(manifest).header(HttpHeaders.ETAG, eTag)
                .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                .header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Credentials", "true")
                .build();
    }
}

From source file:org.opendatakit.api.odktables.InstanceFileService.java

@GET
@Path("manifest")
@Produces({ MediaType.APPLICATION_JSON, ApiConstants.MEDIA_TEXT_XML_UTF8,
        ApiConstants.MEDIA_APPLICATION_XML_UTF8 })
public Response getManifest(@Context HttpHeaders httpHeaders,
        @QueryParam(PARAM_AS_ATTACHMENT) String asAttachment)
        throws IOException, ODKTaskLockException, PermissionDeniedException {
    UriBuilder ub = info.getBaseUriBuilder();
    ub.path(OdkTables.class);
    ub.path(OdkTables.class, "getTablesService");
    UriBuilder full = ub.clone().path(TableService.class, "getRealizedTable")
            .path(RealizedTableService.class, "getInstanceFiles")
            .path(InstanceFileService.class, "getManifest");
    URI self = full.build(appId, tableId, schemaETag, rowId);
    String manifestUrl = self.toURL().toExternalForm();

    // retrieve the incoming if-none-match eTag...
    List<String> eTags = httpHeaders.getRequestHeader(HttpHeaders.IF_NONE_MATCH);
    String eTag = (eTags == null || eTags.isEmpty()) ? null : eTags.get(0);
    DbTableInstanceManifestETagEntity eTagEntity = null;
    try {/*  www . j  a  v  a 2s.  co m*/
        try {
            eTagEntity = DbTableInstanceManifestETags.getRowIdEntry(tableId, rowId, cc);
        } catch (ODKEntityNotFoundException e) {
            // ignore...
        }

        if (eTag != null && eTagEntity != null && eTag.equals(eTagEntity.getManifestETag())) {
            return Response.status(Status.NOT_MODIFIED).header(HttpHeaders.ETAG, eTag)
                    .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                    .header("Access-Control-Allow-Origin", "*")
                    .header("Access-Control-Allow-Credentials", "true").build();
        }

        InstanceFileManager fm = new InstanceFileManager(appId, cc);

        // get the manifest entries
        final TreeMap<String, FileContentInfo> contents = new TreeMap<String, FileContentInfo>();

        fm.getInstanceAttachments(tableId, rowId, new FileContentHandler() {

            @Override
            public void processFileContent(FileContentInfo content, FetchBlobHandler fetcher) {
                contents.put(content.partialPath, content);

            }
        }, userPermissions);

        // transform to the class used in the REST api
        ArrayList<OdkTablesFileManifestEntry> manifestEntries = new ArrayList<OdkTablesFileManifestEntry>();

        for (Map.Entry<String, FileContentInfo> sfci : contents.entrySet()) {
            // these are in sorted order
            OdkTablesFileManifestEntry entry = new OdkTablesFileManifestEntry();
            entry.filename = sfci.getValue().partialPath;
            entry.contentLength = sfci.getValue().contentLength;
            entry.contentType = sfci.getValue().contentType;
            entry.md5hash = sfci.getValue().contentHash;

            URI getFile = ub.clone().path(TableService.class, "getRealizedTable")
                    .path(RealizedTableService.class, "getInstanceFiles")
                    .path(InstanceFileService.class, "getFile")
                    .build(appId, tableId, schemaETag, rowId, entry.filename);
            String locationUrl = getFile.toURL().toExternalForm();
            entry.downloadUrl = locationUrl;

            manifestEntries.add(entry);
        }
        OdkTablesFileManifest manifest = new OdkTablesFileManifest(manifestEntries);

        String newETag = Integer.toHexString(manifest.hashCode());
        // create a new eTagEntity if there isn't one already...
        if (eTagEntity == null) {
            eTagEntity = DbTableInstanceManifestETags.createNewEntity(tableId, rowId, cc);
            eTagEntity.setManifestETag(newETag);
            eTagEntity.put(cc);
        } else if (!newETag.equals(eTagEntity.getManifestETag())) {
            Log log = LogFactory.getLog(FileManifestService.class);
            log.error("TableInstance (" + tableId + "," + rowId
                    + ") Manifest ETag does not match computed value!");
            eTagEntity.setManifestETag(newETag);
            eTagEntity.put(cc);
        }

        // and whatever the eTag is in that entity is the eTag we should return...
        eTag = eTagEntity.getManifestETag();

        ResponseBuilder rBuild = Response.ok(manifest).header(HttpHeaders.ETAG, eTag)
                .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                .header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Credentials", "true");
        if (asAttachment != null && !"".equals(asAttachment)) {
            // Set the filename we're downloading to the disk.
            rBuild.header(WebConsts.CONTENT_DISPOSITION,
                    "attachment; " + "filename=\"" + "manifest.json" + "\"");
        }
        return rBuild.build();
    } catch (ODKDatastoreException e) {
        e.printStackTrace();
        return Response.status(Status.INTERNAL_SERVER_ERROR)
                .entity("Unable to retrieve manifest of attachments for: " + manifestUrl)
                .header(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER, ApiConstants.OPEN_DATA_KIT_VERSION)
                .header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Credentials", "true")
                .build();
    }
}

From source file:org.opendatakit.common.persistence.engine.gae.QueryImpl.java

final static synchronized void updateCostLoggingThreshold(DatastoreImpl datastore) {
    Log logger = LogFactory.getLog(QueryImpl.class);

    long currentTime = System.currentTimeMillis();
    if (milliLastCheck + ACTIVE_COST_LOGGING_CHECK_INTERVAL < currentTime) {

        milliLastCheck = currentTime;// update early in case an exception is
                                     // thrown...
        try {//from   ww  w. j a v a  2  s  .c o m
            com.google.appengine.api.datastore.Query query = new Query("_COST_LOGGING_");
            PreparedQuery pq = datastore.getDatastoreService().prepare(query);
            logger.debug("costLogging fetch.");
            List<com.google.appengine.api.datastore.Entity> eList = pq
                    .asList(FetchOptions.Builder.withDefaults());
            datastore.recordQueryUsage("_COST_LOGGING_", eList.size());
            if (eList.isEmpty()) {
                costLoggingMinimumMegacyclesThreshold = 10 * 1200; // 10 seconds...
                logger.warn("writing 10-second cost logging threshold record");
                com.google.appengine.api.datastore.Entity e = new com.google.appengine.api.datastore.Entity(
                        "_COST_LOGGING_", "T" + WebUtils.iso8601Date(new Date()));
                e.setProperty("COST_LOGGING_MEGACYCLE_THRESHOLD", 10 * 1200); // 10
                                                                              // seconds...
                e.setProperty("LAST_UPDATE_DATE", new Date());
                datastore.getDatastoreService().put(e);
            } else {
                Long newValue = null;
                for (com.google.appengine.api.datastore.Entity e : eList) {
                    Object o = e.getProperty("COST_LOGGING_MEGACYCLE_THRESHOLD");
                    if (o != null) {
                        if (o instanceof Long) {
                            Long l = (Long) o;
                            if (newValue == null || newValue.compareTo(l) > 0) {
                                newValue = l;
                            } else {
                                logger.warn("deleting superceded logging threshold record");
                                datastore.getDatastoreService().delete(e.getKey());
                            }
                        } else if (o instanceof Integer) {
                            Integer i = (Integer) o;
                            Long l = Long.valueOf(i);
                            if (newValue == null || newValue.compareTo(l) > 0) {
                                newValue = l;
                            } else {
                                logger.warn("deleting superceded logging threshold record");
                                datastore.getDatastoreService().delete(e.getKey());
                            }
                        } else if (o instanceof String) {
                            String s = (String) o;
                            try {
                                Long l = Long.parseLong(s);
                                if (newValue == null || newValue.compareTo(l) > 0) {
                                    newValue = l;
                                } else {
                                    logger.warn("deleting superceded logging threshold record");
                                    datastore.getDatastoreService().delete(e.getKey());
                                }
                            } catch (NumberFormatException ex) {
                                logger.warn("deleting superceded logging threshold record");
                                datastore.getDatastoreService().delete(e.getKey());
                            }
                        } else {
                            logger.warn("deleting superceded logging threshold record");
                            datastore.getDatastoreService().delete(e.getKey());
                        }
                    }
                }
                if (newValue == null) {
                    logger.warn("resetting cost logging to 10 second (12000 megacycle) threshold");
                    costLoggingMinimumMegacyclesThreshold = 10 * 1200; // 10 seconds...
                } else if (costLoggingMinimumMegacyclesThreshold != newValue.longValue()) {
                    logger.warn("changing cost logging to " + newValue + " megacycle threshold");
                    costLoggingMinimumMegacyclesThreshold = newValue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("exception while updating cost logging threshold: " + e.getMessage());
        }
    }
}