Example #1
0
  /*
   * (non-Javadoc)
   * @see addressbook.model.PersistenceAccess#load()
   */
  public void load() throws IOException {

    // Read data from given file into DOM document
    final FileInputStream in = new FileInputStream(dataFile);
    Document document = null;
    try {
      document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
    } catch (SAXException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ParserConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    in.close();

    // Put document data in address book
    // Get AddressBookAccess
    Element ab = null;
    if (document.getElementsByTagName("AddressBook").getLength() == 1) {
      ab = (Element) document.getElementsByTagName("AddressBook").item(0);
    } else {
      // Parse error
      throw new IOException("No single AddressBookAccess element found");
    }

    // Read and store entries
    NodeList list = ab.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
      if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {

        Entry entry = new Entry();
        Element el = (Element) list.item(i);

        // Personal data
        entry.setName(el.getAttribute("FirstName"), el.getAttribute("SurName"));
        entry.setGender(
            el.getAttribute("Gender").equalsIgnoreCase("M") ? Gender.Male : Gender.Female);
        entry.setDateOfBirth(Entry.dateFormatter.parseDateTime(el.getAttribute("DateOfBirth")));

        // Contact Information
        if (el.getElementsByTagName("Contact").getLength() > 0) {
          Element contactEl = (Element) el.getElementsByTagName("Contact").item(0);
          if (contactEl.getAttribute("type").equalsIgnoreCase("phone")) {
            entry.setContactPhoneNumber(Integer.parseInt(contactEl.getTextContent()));
          } else if (contactEl.getAttribute("type").equalsIgnoreCase("email")) {
            entry.setContactEmailAddress(contactEl.getTextContent());
          }
        }

        try {
          addEntry(entry);
        } catch (SizeLimitReachedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
 private void readCentralDirectoryEntry(final Map<ZipEntry, NameAndComment> noUTF8Flag)
     throws IOException {
   this.archive.readFully(this.CFH_BUF);
   int off = 0;
   final OffsetEntry offset = new OffsetEntry();
   final Entry ze = new Entry(offset);
   final int versionMadeBy = ZipShort.getValue(this.CFH_BUF, off);
   off += 2;
   ze.setPlatform(versionMadeBy >> 8 & 0xF);
   off += 2;
   final GeneralPurposeBit gpFlag = GeneralPurposeBit.parse(this.CFH_BUF, off);
   final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames();
   final ZipEncoding entryEncoding =
       hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : this.zipEncoding;
   ze.setGeneralPurposeBit(gpFlag);
   off += 2;
   ze.setMethod(ZipShort.getValue(this.CFH_BUF, off));
   off += 2;
   final long time = ZipUtil.dosToJavaTime(ZipLong.getValue(this.CFH_BUF, off));
   ze.setTime(time);
   off += 4;
   ze.setCrc(ZipLong.getValue(this.CFH_BUF, off));
   off += 4;
   ze.setCompressedSize(ZipLong.getValue(this.CFH_BUF, off));
   off += 4;
   ze.setSize(ZipLong.getValue(this.CFH_BUF, off));
   off += 4;
   final int fileNameLen = ZipShort.getValue(this.CFH_BUF, off);
   off += 2;
   final int extraLen = ZipShort.getValue(this.CFH_BUF, off);
   off += 2;
   final int commentLen = ZipShort.getValue(this.CFH_BUF, off);
   off += 2;
   final int diskStart = ZipShort.getValue(this.CFH_BUF, off);
   off += 2;
   ze.setInternalAttributes(ZipShort.getValue(this.CFH_BUF, off));
   off += 2;
   ze.setExternalAttributes(ZipLong.getValue(this.CFH_BUF, off));
   off += 4;
   final byte[] fileName = new byte[fileNameLen];
   this.archive.readFully(fileName);
   ze.setName(entryEncoding.decode(fileName), fileName);
   offset.headerOffset = ZipLong.getValue(this.CFH_BUF, off);
   this.entries.add(ze);
   final byte[] cdExtraData = new byte[extraLen];
   this.archive.readFully(cdExtraData);
   ze.setCentralDirectoryExtra(cdExtraData);
   this.setSizesAndOffsetFromZip64Extra(ze, offset, diskStart);
   final byte[] comment = new byte[commentLen];
   this.archive.readFully(comment);
   ze.setComment(entryEncoding.decode(comment));
   if (!hasUTF8Flag && this.useUnicodeExtraFields) {
     noUTF8Flag.put(ze, new NameAndComment(fileName, comment));
   }
 }
  /**
   * Save the current editor content to the given entry and write to disk.
   *
   * @param entry entry object to save.
   * @throws IOException
   */
  public void saveEntry(final Entry entry) throws IOException {
    if (entry == null) return;
    entry.setName(nameField.getText());
    entry.setComment(commentField.getText());
    entry.setIcon(iconField.getText());
    entry.setExec(execField.getText());
    entry.setType(typeField.getText());
    entry.setCategories(categoryField.getText());
    entry.setTerminal(terminalBox.isSelected());
    entry.setStartupNotify(startupNotifyBox.isSelected());

    FileWriter writer = new FileWriter(entry.getFile());
    writer.write(entry.toContentString());
    writer.close();

    JOptionPane.showMessageDialog(null, entry.getFile().getName() + " has been saved!");
  }
Example #4
0
  /**
   * Creates a new file in the config/upgrade folder. The new file is a concatenation of entries of
   * all files contained in the config/schema folder.
   *
   * @param folder The folder containing the schema files.
   * @param revision The revision number of the current binary version.
   * @throws Exception If we cannot read the files contained in the folder where the schema files
   *     are supposed to be, or the file has errors.
   */
  static void updateConfigUpgradeSchemaFile(final File folder, final String revision)
      throws Exception {
    // We need to upgrade the schema.ldif.<rev> file contained in the
    // config/upgrade folder otherwise, we cannot enable the backend at
    // server's start. We need to read all files contained in config/schema
    // and add all attribute/object classes in this new super entry which
    // will be read at start-up.
    Entry theNewSchemaEntry = new LinkedHashMapEntry();
    LDIFEntryReader reader = null;
    LDIFEntryWriter writer = null;
    try {
      if (folder.isDirectory()) {
        final FilenameFilter filter = new SchemaConfigManager.SchemaFileFilter();
        for (final File f : folder.listFiles(filter)) {
          logger.debug(LocalizableMessage.raw(String.format("Processing %s", f.getAbsolutePath())));
          reader = new LDIFEntryReader(new FileInputStream(f));
          try {
            while (reader.hasNext()) {
              final Entry entry = reader.readEntry();
              theNewSchemaEntry.setName(entry.getName());
              for (final Attribute at : entry.getAllAttributes()) {
                theNewSchemaEntry.addAttribute(at);
              }
            }
          } catch (Exception ex) {
            throw new Exception(
                "Error parsing existing schema file " + f.getName() + " - " + ex.getMessage(), ex);
          }
        }

        // Creates a File object representing
        // config/upgrade/schema.ldif.revision which the server creates
        // the first time it starts if there are schema customizations.
        final File destination =
            new File(
                configDirectory,
                Installation.UPGRADE_PATH + File.separator + "schema.ldif." + revision);

        // Checks if the parent exists (eg. embedded
        // server doesn't seem to provide that folder)
        File parentDirectory = destination.getParentFile();
        if (!parentDirectory.exists()) {
          logger.debug(
              LocalizableMessage.raw(
                  String.format("Parent file of %s doesn't exist", destination.getPath())));

          parentDirectory.mkdirs();

          logger.debug(
              LocalizableMessage.raw(
                  String.format("Parent directory %s created.", parentDirectory.getPath())));
        }
        if (!destination.exists()) {
          destination.createNewFile();
        }

        logger.debug(
            LocalizableMessage.raw(
                String.format("Writing entries in %s.", destination.getAbsolutePath())));

        writer = new LDIFEntryWriter(new FileOutputStream(destination));
        writer.writeEntry(theNewSchemaEntry);

        logger.debug(
            LocalizableMessage.raw(
                String.format(
                    "%s created and completed successfully.", destination.getAbsolutePath())));
      }
    } finally {
      StaticUtils.close(reader, writer);
    }
  }
Example #5
0
  /**
   * Updates the entry based on the field that is specified. Mainly created for use by the bulk
   * import auto update
   *
   * @param entry entry to be updated
   * @param plasmid should be set if updating strain with plasmid
   * @param value value to be set
   * @param field to set
   * @return updated entry array containing both entry and plasmid. if plasmid is null only entry is
   *     returned
   */
  public static Entry[] infoToEntryForField(
      Entry entry, Entry plasmid, String value, EntryField field) {
    switch (field) {
      case PI:
        {
          entry.setPrincipalInvestigator(value);
          if (plasmid != null) plasmid.setPrincipalInvestigator(value);
          break;
        }

      case PI_EMAIL:
        {
          entry.setPrincipalInvestigatorEmail(value);
          break;
        }

      case FUNDING_SOURCE:
        {
          entry.setFundingSource(value);
          if (plasmid != null) plasmid.setFundingSource(value);
          break;
        }

      case IP:
        entry.setIntellectualProperty(value);
        if (plasmid != null) plasmid.setIntellectualProperty(value);
        break;

      case BIO_SAFETY_LEVEL:
        Integer level = BioSafetyOption.intValue(value);
        if (level == null) {
          if (value.contains("1")) level = 1;
          else if (value.contains("2")) level = 2;
          else break;
        }
        entry.setBioSafetyLevel(level);
        if (plasmid != null) {
          plasmid.setBioSafetyLevel(level);
        }
        break;

      case NAME:
        entry.setName(value);
        break;

      case ALIAS:
        entry.setAlias(value);
        break;

      case KEYWORDS:
        entry.setKeywords(value);
        break;

      case SUMMARY:
        entry.setShortDescription(value);
        break;

      case NOTES:
        entry.setLongDescription(value);
        break;

      case REFERENCES:
        entry.setReferences(value);
        break;

      case LINKS:
        HashSet<Link> links = new HashSet<>();
        Link link = new Link();
        link.setLink(value);
        link.setEntry(entry);
        links.add(link);
        entry.setLinks(links);
        break;

      case STATUS:
        entry.setStatus(value);
        if (plasmid != null) plasmid.setStatus(value);
        break;

      case SELECTION_MARKERS:
        HashSet<SelectionMarker> markers = new HashSet<>();
        SelectionMarker marker = new SelectionMarker(value, entry);
        markers.add(marker);
        entry.setSelectionMarkers(markers);
        break;

      case PARENTAL_STRAIN:
      case GENOTYPE_OR_PHENOTYPE:
      case PLASMIDS:
        entry = infoToStrainForField(entry, value, field);
        break;

      case BACKBONE:
      case ORIGIN_OF_REPLICATION:
      case CIRCULAR:
      case PROMOTERS:
      case REPLICATES_IN:
        entry = infoToPlasmidForField(entry, value, field);
        break;

      case HOMOZYGOSITY:
      case ECOTYPE:
      case HARVEST_DATE:
      case GENERATION:
      case SENT_TO_ABRC:
      case PLANT_TYPE:
      case PARENTS:
        entry = infoToSeedForField(entry, value, field);
        break;
      default:
        break;
    }
    if (plasmid == null) return new Entry[] {entry};

    return new Entry[] {entry, plasmid};
  }
Example #6
0
  private static Entry setCommon(Entry entry, PartData info) {
    if (entry == null || info == null) return null;

    if (info.getName() != null) entry.setName(info.getName());

    if (info.getSelectionMarkers() != null) {
      Set<SelectionMarker> markers = getSelectionMarkers(info.getSelectionMarkers(), entry);
      entry.setSelectionMarkers(markers);
    }

    if (info.getReferences() != null) entry.setReferences(info.getReferences());

    if (StringUtils.isBlank(entry.getPartNumber())) entry.setPartNumber(info.getPartId());

    Date currentTime = new Date();
    if (entry.getCreationTime() == null) entry.setCreationTime(currentTime);

    entry.setModificationTime(currentTime);

    if (info.getOwnerEmail() != null) {
      entry.setOwnerEmail(info.getOwnerEmail());
    }

    if (info.getOwner() != null) entry.setOwner(info.getOwner());

    if (info.getCreatorEmail() != null) {
      entry.setCreatorEmail(info.getCreatorEmail());
    }

    if (info.getCreator() != null) entry.setCreator(info.getCreator());

    if (info.getStatus() == null) {
      if (StringUtils.isBlank(entry.getStatus())) entry.setStatus("");
    } else entry.setStatus(info.getStatus());

    if (info.getAlias() != null) entry.setAlias(info.getAlias());

    if (info.getBioSafetyLevel() == null) {
      if (entry.getBioSafetyLevel() == null) entry.setBioSafetyLevel(0);
    } else entry.setBioSafetyLevel(info.getBioSafetyLevel());

    if (info.getShortDescription() != null) entry.setShortDescription(info.getShortDescription());
    if (info.getLongDescription() != null) entry.setLongDescription(info.getLongDescription());
    if (info.getIntellectualProperty() != null)
      entry.setIntellectualProperty(info.getIntellectualProperty());

    Set<Link> links = getLinks(info.getLinks(), entry);
    entry.setLinks(links);

    Visibility visibility = info.getVisibility();
    if (visibility != null) entry.setVisibility(visibility.getValue());

    // checking for null instead of blank since it could be cleared
    if (info.getFundingSource() != null) entry.setFundingSource(info.getFundingSource());
    if (info.getPrincipalInvestigator() != null)
      entry.setPrincipalInvestigator(info.getPrincipalInvestigator());
    if (info.getPrincipalInvestigatorEmail() != null)
      entry.setPrincipalInvestigatorEmail(info.getPrincipalInvestigatorEmail());

    if (info.getKeywords() != null) entry.setKeywords(info.getKeywords());

    // parameters
    List<Parameter> parameters = getParameters(info.getCustomFields(), entry);
    entry.setParameters(parameters);
    return entry;
  }