/* (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#doAdd(uk.ac.ucl.excites.sapelli.collector.model.Project) */ @Override protected void doAdd(Project project) throws ProjectIdentificationClashException, ProjectSignatureClashException, IllegalStateException { try { // Note: we use insert() instead of store() to not allow updates // Store project record: rsWrapper.recordStore.insert(getProjectRecord(project)); // Store an FSI record for each record-producing form: for (Form form : project.getForms()) if (form.isProducesRecords()) rsWrapper.recordStore.insert(getFSIRecord(form)); // Cache the project: cacheProject(project); } catch (DBPrimaryKeyException dbPKE) { throw new ProjectIdentificationClashException(project, false); } catch (DBConstraintException dbCE) { throw new ProjectSignatureClashException(project); } catch (DBException e) { e.printStackTrace(System.err); throw new IllegalStateException(e); } }
@Override protected void parseEndElement(String uri, String localName, String qName) throws Exception { // </Sapelli-Collector-Project>, or </ExCiteS-Collector-Project> (for backwards compatibility) if (qName.equals(TAG_PROJECT) || qName.equals(TAG_PROJECT_V1X)) { clearSubtreeParsers(); if (project.getForms().size() == 0) throw new SAXException("A project such have at least 1 form!"); else { // Resolve startForm Form startForm = project.getForm( startFormID); // will return null if startFormID is null or there is no form with // that name, uses equalsIgnoreCase() if (startForm != null) project.setStartForm(startForm); // else: first form of project will remain the startForm // Resolve form relationships: if (relationshipToFormID != null) for (Entry<Relationship, String> entry : relationshipToFormID.entrySet()) { Relationship rel = entry.getKey(); Form relatedForm = project.getForm(entry.getValue()); // uses equalsIgnoreCase() if (relatedForm == null) throw new SAXException( "Relationship \"" + rel.id + "\" in form \"" + rel.form.id + "\" refers to unknown related form \"" + entry.getValue() + "\"."); rel.setRelatedForm( relatedForm); // will trigger initialisation of Schema of relatedForm (this should // not be a problem, it will not be done again below) } // Initialise forms... for (Form form : project.getForms()) { try { // generates Schema, Columns & ValueDictionaries: form.initialiseStorage( fsiProvider != null ? fsiProvider.getByPassableFieldIDs(form) : null); // Note: fsiProvider will be null if this project is loaded/parsed for // the first time } catch (ModelFullException mfe) { throw new SAXException( "This project contains more data-producing Forms than allowed (maximum: " + Project.MAX_RECORD_PRODUCING_FORMS + ")."); } catch (DuplicateColumnException dce) { throw new SAXException( "Duplicate column name (\"" + dce.getColumnName() + "\") in schema for form \"" + form.id + "\"."); } addWarnings(form.getWarnings()); // !!! form.clearWarnings(); } // Seal project model: project.getModel().seal(); // Resolve relationship constraints: if (relationshipToConstraints != null) for (Entry<Relationship, List<ConstraintDescription>> entry : relationshipToConstraints.entrySet()) for (ConstraintDescription constrDesc : entry.getValue()) try { Relationship rel = entry.getKey(); rel.addConstraint(constrDesc.resolve(rel.getRelatedForm())); } catch (Exception e) { throw new Exception( "Error upon resolving constraint on Relationship \"" + entry.getKey().id + "\"", e); } } } }