示例#1
0
 private void updateProjectCache() {
   // Ensure we have a cache at all:
   initProjectCache();
   // Loop through project path preference keys:
   for (Map.Entry<String, ?> entry : preferences.getAll().entrySet())
     if (entry.getKey().startsWith(PREF_PROJECT_PATH_PREFIX)
         && entry.getKey().endsWith(PREF_PROJECT_PATH_POSTFIX)) {
       int projectID = getProjectID(entry.getKey());
       int projectFingerPrint = getProjectFingerPrint(entry.getKey());
       if (getCachedProject(projectID, projectFingerPrint)
           == null) { // Parse the project if it is not already in the cache:
         Project p = ProjectLoader.ParseProject(entry.getValue().toString());
         if (p != null) {
           if (p.getFingerPrint() != projectFingerPrint) {
             Log.w(
                 TAG,
                 "XML finger print of project "
                     + p.toString()
                     + " has changed, possibly the "
                     + ProjectLoader.PROJECT_FILE
                     + " file (located in "
                     + entry.getValue().toString()
                     + ") was manually edited!");
             // Remove old pref key:
             removeProjectPathPrefKey(projectID, projectFingerPrint);
             // Add new pref key:
             storeProjectPathPrefKey(p);
           }
           // Cache the project object:
           cacheProject(p);
         }
       }
     }
 }
  /* (non-Javadoc)
   * @see uk.ac.ucl.excites.sapelli.collector.db.ProjectStore#serialise(uk.ac.ucl.excites.sapelli.collector.model.Project, java.io.OutputStream)
   */
  @Override
  public void serialise(Project project, OutputStream out) throws IOException {
    // Project XML bytes:
    InputStream projectXMLFileIn =
        new FileInputStream(ProjectLoader.GetProjectXMLFile(getProjectFolder(project)));
    ByteArrayOutputStream projectXMLBytesOut = new ByteArrayOutputStream();
    IOUtils.copy(projectXMLFileIn, projectXMLBytesOut);
    projectXMLFileIn.close();
    projectXMLBytesOut.close();

    // FSI record bytes for each Form:
    List<byte[]> fsiRecordBytesList = new ArrayList<>(project.getNumberOfForms());
    for (Form form : project.getForms()) {
      Record fsiRecord = retrieveFSIRecord(form); // we query the projectStore to save time...
      if (fsiRecord == null) // ... but we don't rely on it:
      fsiRecord = getFSIRecord(form); // may trigger optionality analysis
      fsiRecordBytesList.add(fsiRecord.toBytes(true));
    }

    // Create serialisedProject valueSet:
    ValueSet<ColumnSet> serialisedProjectVS =
        new ValueSet<ColumnSet>(
            PROJECT_SERIALISIATION_CS, projectXMLBytesOut.toByteArray(), fsiRecordBytesList);

    // Return as byte array:
    BitOutputStream bitsOut = new BitWrapOutputStream(out);
    serialisedProjectVS.writeToBitStream(bitsOut, true);
    bitsOut.flush();
  }
  /* (non-Javadoc)
   * @see uk.ac.ucl.excites.sapelli.collector.db.ProjectStore#deserialise(java.io.InputStream)
   */
  @Override
  public Project deserialise(InputStream in) throws IOException {
    // Deserialise valueSet:
    BitInputStream bitsIn = new BitWrapInputStream(in);
    ValueSet<ColumnSet> serialisedProjectVS = new ValueSet<ColumnSet>(PROJECT_SERIALISIATION_CS);
    serialisedProjectVS.readFromBitStream(bitsIn, true);
    bitsIn.close();

    // Retrieve FSI records:
    List<byte[]> fsiRecordBytesList =
        PROJECT_SERIALISIATION_FSI_RECORDS_COLUMN.retrieveValue(serialisedProjectVS);
    final List<Record> fsiRecords = new ArrayList<Record>(fsiRecordBytesList.size());
    for (byte[] fsiRecordBytes : fsiRecordBytesList)
      fsiRecords.add(FSI_SCHEMA.createRecord(fsiRecordBytes, true));

    // Retrieve & parse Project XML:
    return ProjectLoader.ParseProjectXML(
        new ByteArrayInputStream(
            PROJECT_SERIALISIATION_XML_COLUMN.retrieveValue(serialisedProjectVS)),
        new FormSchemaInfoProvider() {
          @Override
          public List<String> getByPassableFieldIDs(Form form) {
            for (Record fsiRecord : fsiRecords)
              if (FSI_FORM_POSITION_COLUMN.retrieveValue(fsiRecord).shortValue()
                  == form.getPosition())
                return ProjectRecordStore.this.getByPassableFieldIDs(fsiRecord);
            return null;
          }
        });
  }
  private Project loadProject(ProjectDescriptor projDescr) {
    if (projDescr == null) return null;

    // If the ProjectDescriptor is a full Project:
    if (projDescr instanceof Project) return (Project) projDescr;

    // Load project...
    Project project = null;

    // First check the cache:
    project = cache.get(getCacheKey(projDescr));

    // Parse project if we didn't get it from the cache:
    if (project == null) {
      project =
          ProjectLoader.ParseProjectXMLInFolder(
              getProjectFolder(projDescr), this); // pass this as FormSchemaInfoProvider
      // Check if we have a project:
      if (project == null)
        // If not, delete the project:
        delete(projDescr);
      else
        // Add to cache:
        cacheProject((Project) project);
    }
    return project;
  }
示例#5
0
  /**
   * Retrieves specific Project
   *
   * @return null if project was not found
   * @see uk.ac.ucl.excites.sapelli.collector.db.ProjectStore#retrieveProject(int, int)
   */
  @Override
  public Project retrieveProject(int projectID, int projectFingerPrint) {
    // Get project from cache if it is there ...
    Project cachedProject = getCachedProject(projectID, projectFingerPrint);
    if (cachedProject != null) return cachedProject;

    // ... parse the project if not ...
    String folderPath =
        preferences.getString(getProjectPathPrefKey(projectID, projectFingerPrint), null);
    if (folderPath != null) {
      Project p = ProjectLoader.ParseProject(folderPath);
      if (p != null) {
        cacheProject(p); // cache the project (the cache will be initialised if needed)
        return p;
      } else
        removeProjectPathPrefKey(
            projectID,
            projectFingerPrint); // we were unable to parse a project at the path, so remove it from
                                 // the preferences
    }

    // Project not found:
    return null;
  }