List of usage examples for java.math BigInteger ONE
BigInteger ONE
To view the source code for java.math BigInteger ONE.
Click Source Link
From source file:net.pms.util.Rational.java
/** * Used internally to generate a decimal string representation from two * {@link BigInteger}s. The decimal representation is limited to 20 * decimals.//ww w .j a v a2s .c o m * * @param numerator the numerator. * @param denominator the denominator. * @param decimalFormat the {@link DecimalFormat} instance to use for * formatting. * @return The string representation. */ protected static String generateDecimalString(@Nonnull BigInteger numerator, @Nonnull BigInteger denominator, @Nonnull DecimalFormat decimalFormat) { if (denominator.signum() == 0) { if (numerator.signum() == 0) { return "NaN"; } return numerator.signum() > 0 ? "\u221e" : "-\u221e"; } if (BigInteger.ONE.equals(denominator)) { return numerator.toString(); } BigDecimal decimalValue = new BigDecimal(numerator).divide(new BigDecimal(denominator), 20, RoundingMode.HALF_EVEN); return decimalFormat.format(decimalValue); }
From source file:org.nd4j.linalg.util.BigDecimalMath.java
/** * Broadhurst ladder sequence./*from ww w . jav a 2 s .co m*/ * * @param a The vector of 8 integer arguments * @param mc Specification of the accuracy of the result * @return S_(n, p)(a) * @see \protect\vrule width0pt\protect\href{http://arxiv.org/abs/math/9803067}{arXiv:math/9803067} */ static protected BigDecimal broadhurstBBP(final int n, final int p, final int a[], MathContext mc) { /* Explore the actual magnitude of the result first with a quick estimate. */ double x = 0.0; for (int k = 1; k < 10; k++) { x += a[(k - 1) % 8] / Math.pow(2., p * (k + 1) / 2) / Math.pow((double) k, n); } /* Convert the relative precision and estimate of the result into an absolute precision. */ double eps = prec2err(x, mc.getPrecision()); /* Divide this through the number of terms in the sum to account for error accumulation * The divisor 2^(p(k+1)/2) means that on the average each 8th term in k has shrunk by * relative to the 8th predecessor by 1/2^(4p). 1/2^(4pc) = 10^(-precision) with c the 8term * cycles yields c=log_2( 10^precision)/4p = 3.3*precision/4p with k=8c */ int kmax = (int) (6.6 * mc.getPrecision() / p); /* Now eps is the absolute error in each term */ eps /= kmax; BigDecimal res = BigDecimal.ZERO; for (int c = 0;; c++) { Rational r = new Rational(); for (int k = 0; k < 8; k++) { Rational tmp = new Rational(new BigInteger("" + a[k]), (new BigInteger("" + (1 + 8 * c + k))).pow(n)); /* floor( (pk+p)/2) */ int pk1h = p * (2 + 8 * c + k) / 2; tmp = tmp.divide(BigInteger.ONE.shiftLeft(pk1h)); r = r.add(tmp); } if (Math.abs(r.doubleValue()) < eps) { break; } MathContext mcloc = new MathContext(1 + err2prec(r.doubleValue(), eps)); res = res.add(r.BigDecimalValue(mcloc)); } return res.round(mc); }
From source file:org.proteomecommons.tranche.cacheupdater.CacheUpdater.java
private void updateTagsDatabase() { log.println("Starting to update the tags db"); log.println("Deleting entries with no associated tags."); try {//from w ww .j av a 2 s .c o m // check every entry List<Entry> entries = Database.getEntries(); for (Entry entry : entries) { try { int size = Database.getTags(entry.getId()).size(); if (size == 0) { Database.deleteEntry(entry.getId()); log.println("Deleted entry #" + entry.getId()); } } catch (Exception e) { log.println("ERROR: " + e.getMessage()); err.println(e.getMessage()); } } } catch (Exception e) { log.println("ERROR: " + e.getMessage()); err.println(e.getMessage()); } log.println("Correcting tag names in all entries."); try { // check for and update tag names according to the TagNames list for (String name : TagNames.changedNames.keySet()) { for (Tag tag : Database.getTags(name)) { removedTag(tag.getEntryId(), name, tag.getValue().trim()); addedTag(tag.getEntryId(), TagNames.changedNames.get(name), tag.getValue().trim()); Database.updateTag(tag.getId(), TagNames.changedNames.get(name), tag.getValue().trim()); } for (Tag tag : Database.getTags(name + "%")) { // make sure there was not another tag name that starts the same ("Tranche:Link" and "Tranche:Link Name") String following = ""; { // if there is more to the tag name than the one we are looking for if (tag.getName().length() > name.length()) { // skip this tag if what is left is not a number try { Long.valueOf(tag.getName().substring(name.length()).trim()); following = tag.getName().substring(name.length()); } catch (Exception e) { break; } } } // get the new name String newName = TagNames.changedNames.get(name) + following; removedTag(tag.getEntryId(), name, tag.getValue().trim()); addedTag(tag.getEntryId(), newName, tag.getValue().trim()); Database.updateTag(tag.getId(), newName, tag.getValue().trim()); } } } catch (Exception e) { log.println("ERROR: An unknown problem occurred revising tag names."); err.println(e.getMessage()); } log.println("Making sure all tags that start with \"Tranche:\" are accompanied by a number."); try { // check every entry List<Entry> entries = Database.getEntries(); for (Entry entry : entries) { // create a data structure for the set of tags Map<String, List<String>> tagMap = makeTagMap(entry.getId()); // for all other links that start with "Tranche:", if it does not end in a number, then move it to the first available spot List<String> toRemove = new ArrayList<String>(); Map<String, String> toAdd = new HashMap<String, String>(); for (String name : tagMap.keySet()) { if (name.startsWith("Tranche:")) { // stop here if the last part of this tag is a number try { Long.valueOf(name.split(" ")[name.split(" ").length - 1].trim()); // go to the next tag continue; } catch (Exception e) { } int index = 1; for (String value : tagMap.get(name)) { String newName = name + " " + index; // find an empty number while (tagMap.get(newName) != null) { index++; newName = name + " " + index; } removedTag(entry.getId(), name, value.trim()); toAdd.put(newName, value.trim()); addedTag(entry.getId(), newName, value.trim()); } toRemove.add(name); } } // implement the removal the tags for (String name : toRemove) { tagMap.remove(name); } // implement the addition of tags for (String name : toAdd.keySet()) { if (tagMap.get(name) == null) { tagMap.put(name, new ArrayList<String>()); } tagMap.get(name).add(toAdd.get(name)); } // update the database try { // delete all the old tags if (makeChanges) { Database.deleteTags(entry.getId()); } // add all the tags for (String tagName : tagMap.keySet()) { for (String tagValue : tagMap.get(tagName)) { if (makeChanges) { Database.addTag(entry.getId(), tagName, tagValue.trim()); } } } } catch (Exception e) { log.println("ERROR: There was a problem changing the database."); err.println(e.getMessage()); } } } catch (Exception e) { log.println("ERROR: An unknown problem occurred switching bad Tranche tag names."); err.println(e.getMessage()); } log.println("Updating tags for all existing entries."); try { // for all of the entries that have a tag with the name that starts with TagNames.TRANCHE_LINK for (Entry entry : Database.getEntries()) { log.println("Updating the tags for entry " + entry.getId()); // create a data structure for the set of tags Map<String, List<String>> tagMap = makeTagMap(entry.getId()); // get the number of links in this entry int links = getNumberOfLinks(tagMap); log.println(links + " Tranche Links found in the entry."); // for all of the tranche links for (int linkNum = 1; linkNum <= links; linkNum++) { try { // try to make the hash for this tranche link BigHash hash = null; try { hash = BigHash .createHashFromString(tagMap.get(TagNames.TRANCHE_LINK + " " + linkNum).get(0)); // remember that this hash is in the tags db hashesInTags.add(hash); } catch (Exception e) { // bad hash - remove it for (String value : tagMap.get(TagNames.TRANCHE_LINK + " " + linkNum)) { removedTag(entry.getId(), TagNames.TRANCHE_LINK + " " + linkNum, value); } tagMap.remove(TagNames.TRANCHE_LINK + " " + linkNum); editedEntries.add(entry.getId()); // update the database saveToDatabase(entry.getId(), tagMap); continue; } log.println("Trying hash: " + hash.toString()); // set up for the update MetaData md = null; ProjectFile pf = null; GetFileTool gft = null; // need to know if the meta data has changed boolean metaDataChanged = false; try { // set up the getfiletool gft = new GetFileTool(); gft.setValidate(validate); gft.setHash(hash); // increment the chunk meta count by the meta data chunkAndMetaCount++; // get the meta data if (md == null) { try { md = gft.getMetaData(); } catch (CantVerifySignatureException e) { addInvalid(entry.getId(), hash.toString(), tagMap, linkNum, e); log.println("ERROR: Downloaded meta data is invalid."); } catch (Exception e) { if (e.getMessage() != null && e.getMessage().toLowerCase().contains("can't find metadata")) { addMissing(entry.getId(), hash.toString(), tagMap, linkNum); log.println("ERROR: Could not get meta data."); } else { addInvalid(entry.getId(), hash.toString(), tagMap, linkNum, e); log.println("ERROR: Downloaded meta data is invalid."); } } } } catch (Exception e) { err.println(e.getMessage()); } // tags that require the meta data to check or add if (md != null) { // make sure there only valid share meta data if encrypted annotations for (MetaDataAnnotation mda : md.getAnnotations()) { // remove if this is not a valid share md if encrypted annotation if (!mda.getName().equals( MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION.getName()) && mda.getValue().toLowerCase() .equals(MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION .getValue().toLowerCase())) { removedTag(entry.getId(), hash, MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION.getName(), MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } } // sift through all of the meta data annotations for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals( MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION.getName())) { if (tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum, new ArrayList()); } // should only be one entry if (tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).size() > 1) { // remove all but the first entry for this tag name for (int i = tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum) .size() - 1; i > 0; i--) { String removedValue = tagMap .get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).remove(i); removedTag(entry.getId(), hash, TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum, removedValue, false); } } // in case there are no entries, just add the tag if (tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).size() == 0) { // add the tag tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum) .add(mda.getValue().trim()); addedTag(entry.getId(), hash, TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum, mda.getValue(), false); } else if (tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum) .size() == 1) { if (!tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).get(0) .equals(mda.getValue().trim())) { // edit the tag editedTag(entry.getId(), hash, TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum, tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum) .get(0), mda.getValue(), false); tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum) .add(mda.getValue().trim()); } } } else if (mda.getName().equals(MetaDataAnnotation.PROP_DELETE_NEW_VERSION)) { if (tagMap.get(TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum, new ArrayList()); } if (!tagMap.get(TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum) .contains(mda.getValue())) { try { BigHash deleteNewVersionHash = BigHash .createHashFromString(mda.getValue()); tagMap.get(TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum) .add(deleteNewVersionHash.toString()); addedTag(entry.getId(), hash, TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum, deleteNewVersionHash.toString(), false); if (!metaDataContainsAnnotation(deleteNewVersionHash, MetaDataAnnotation.PROP_DELETE_OLD_VERSION, hash.toString())) { addMetaDataAnnotationNow(deleteNewVersionHash, MetaDataAnnotation.PROP_DELETE_OLD_VERSION, hash.toString()); } } catch (Exception e) { // the hash is bad - remove the annotation removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_DELETE_NEW_VERSION, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } } } else if (mda.getName().equals(MetaDataAnnotation.PROP_DELETE_OLD_VERSION)) { if (tagMap.get(TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum, new ArrayList()); } if (!tagMap.get(TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum) .contains(mda.getValue())) { try { BigHash deleteOldVersionHash = BigHash .createHashFromString(mda.getValue()); tagMap.get(TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum) .add(deleteOldVersionHash.toString()); addedTag(entry.getId(), hash, TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum, deleteOldVersionHash.toString(), false); if (!metaDataContainsAnnotation(deleteOldVersionHash, MetaDataAnnotation.PROP_DELETE_NEW_VERSION, hash.toString())) { addMetaDataAnnotationNow(deleteOldVersionHash, MetaDataAnnotation.PROP_DELETE_NEW_VERSION, hash.toString()); } } catch (Exception e) { // the hash is bad - remove the annotation removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_DELETE_OLD_VERSION, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } } } else if (mda.getName().equals(MetaDataAnnotation.PROP_UNDELETED)) { if (tagMap.get(TagNames.TRANCHE_UNDELETED + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_UNDELETED + " " + linkNum, new ArrayList()); } if (!tagMap.get(TagNames.TRANCHE_UNDELETED + " " + linkNum) .contains(mda.getValue())) { try { Long undeletedTimestamp = Long.valueOf(mda.getValue()); tagMap.get(TagNames.TRANCHE_UNDELETED + " " + linkNum) .add(String.valueOf(undeletedTimestamp)); addedTag(entry.getId(), hash, TagNames.TRANCHE_UNDELETED + " " + linkNum, String.valueOf(undeletedTimestamp), false); } catch (Exception e) { // the value is not a timestamp - remove it removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_UNDELETED, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } } } else if (mda.getName().equals(MetaDataAnnotation.PROP_NEW_VERSION)) { if (tagMap.get(TagNames.TRANCHE_NEW_LINK + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_NEW_LINK + " " + linkNum, new ArrayList()); } if (!tagMap.get(TagNames.TRANCHE_NEW_LINK + " " + linkNum) .contains(mda.getValue().trim())) { try { BigHash newVersionHash = BigHash.createHashFromString(mda.getValue()); tagMap.get(TagNames.TRANCHE_NEW_LINK + " " + linkNum) .add(newVersionHash.toString()); addedTag(entry.getId(), hash, TagNames.TRANCHE_NEW_LINK + " " + linkNum, newVersionHash.toString(), false); if (!metaDataContainsAnnotation(newVersionHash, MetaDataAnnotation.PROP_OLD_VERSION, hash.toString())) { addMetaDataAnnotationNow(newVersionHash, MetaDataAnnotation.PROP_OLD_VERSION, hash.toString()); } } catch (Exception e) { // the hash is bad - remove the annotation removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_NEW_VERSION, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } } } else if (mda.getName().equals(MetaDataAnnotation.PROP_OLD_VERSION)) { if (tagMap.get(TagNames.TRANCHE_OLD_LINK + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_OLD_LINK + " " + linkNum, new ArrayList()); } if (!tagMap.get(TagNames.TRANCHE_OLD_LINK + " " + linkNum) .contains(mda.getValue().trim())) { try { BigHash oldVersionHash = BigHash.createHashFromString(mda.getValue()); tagMap.get(TagNames.TRANCHE_OLD_LINK + " " + linkNum) .add(oldVersionHash.toString()); addedTag(entry.getId(), hash, TagNames.TRANCHE_OLD_LINK + " " + linkNum, oldVersionHash.toString(), false); if (!metaDataContainsAnnotation(oldVersionHash, MetaDataAnnotation.PROP_NEW_VERSION, hash.toString())) { addMetaDataAnnotationNow(oldVersionHash, MetaDataAnnotation.PROP_NEW_VERSION, hash.toString()); } } catch (Exception e) { // the hash is bad - remove the annotation removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_OLD_VERSION, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } } } } // resolve conflicts between delete/undelete tags if (tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum) != null && tagMap.get(TagNames.TRANCHE_UNDELETED + " " + linkNum) != null) { // remember the action that last occurred String latestTagName = ""; long latestActionTaken = 0; for (String value : tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum)) { try { if (Long.valueOf(value) > latestActionTaken) { latestTagName = TagNames.TRANCHE_DELETED + " " + linkNum; latestActionTaken = Long.valueOf(value); } } catch (Exception e) { } } for (String value : tagMap.get(TagNames.TRANCHE_UNDELETED + " " + linkNum)) { try { if (Long.valueOf(value) > latestActionTaken) { latestTagName = TagNames.TRANCHE_UNDELETED + " " + linkNum; latestActionTaken = Long.valueOf(value); } } catch (Exception e) { } } for (String value : tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum)) { removedTag(entry.getId(), hash, TagNames.TRANCHE_DELETED + " " + linkNum, value, false); } for (String value : tagMap.get(TagNames.TRANCHE_UNDELETED + " " + linkNum)) { removedTag(entry.getId(), hash, TagNames.TRANCHE_UNDELETED + " " + linkNum, value, false); } tagMap.remove(TagNames.TRANCHE_DELETED + " " + linkNum); tagMap.remove(TagNames.TRANCHE_UNDELETED + " " + linkNum); // only put it back if it's a deleted tag if (latestTagName.equals(TagNames.TRANCHE_DELETED + " " + linkNum)) { tagMap.put(latestTagName, new ArrayList<String>()); tagMap.get(latestTagName).add(String.valueOf(latestActionTaken)); addedTag(entry.getId(), hash, latestTagName, String.valueOf(latestActionTaken), false); boolean found = false; // make sure the meta data annotations have no undeleted annotations for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(mda.PROP_UNDELETED)) { removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_UNDELETED, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } else if (mda.getName().equals(mda.PROP_DELETED) && !mda.getValue().equals(String.valueOf(latestActionTaken))) { removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_DELETED, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } else if (mda.getName().equals(mda.PROP_DELETED) && mda.getValue().equals(String.valueOf(latestActionTaken))) { found = true; } } if (!found) { md.addAnnotation(new MetaDataAnnotation(MetaDataAnnotation.PROP_DELETED, String.valueOf(latestActionTaken))); metaDataChanged = true; addedTag(entry.getId(), hash, MetaDataAnnotation.PROP_DELETED, String.valueOf(latestActionTaken), true); } } else if (latestTagName.equals(TagNames.TRANCHE_UNDELETED + " " + linkNum)) { // make sure the meta data annotations have no undeleted annotations for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(mda.PROP_UNDELETED)) { removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_UNDELETED, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } else if (mda.getName().equals(mda.PROP_DELETED)) { removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_DELETED, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); } } } } // resolve conflicts between new link/delete new link tags if (tagMap.get(TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum) != null) { for (String deleteValue : tagMap .get(TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum)) { // remove new links with the same value if (tagMap.get(TagNames.TRANCHE_NEW_LINK + " " + linkNum) != null) { if (tagMap.get(TagNames.TRANCHE_NEW_LINK + " " + linkNum) .remove(deleteValue.trim())) { removedTag(entry.getId(), hash, TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum, deleteValue, false); } } removedTag(entry.getId(), hash, TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum, deleteValue.trim(), false); } tagMap.remove(TagNames.TRANCHE_DELETE_NEW_LINK + " " + linkNum); } // resolve conflicts between old link/delete old link tags if (tagMap.get(TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum) != null) { for (String deleteValue : tagMap .get(TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum)) { // remove old links with the same value if (tagMap.get(TagNames.TRANCHE_OLD_LINK + " " + linkNum) != null) { if (tagMap.get(TagNames.TRANCHE_OLD_LINK + " " + linkNum) .remove(deleteValue.trim())) { removedTag(entry.getId(), hash, TagNames.TRANCHE_OLD_LINK + " " + linkNum, deleteValue.trim(), false); } } removedTag(entry.getId(), hash, TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum, deleteValue.trim(), false); } tagMap.remove(TagNames.TRANCHE_DELETE_OLD_LINK + " " + linkNum); } // make sure the meta data has all the old/new links from tags if (tagMap.get(TagNames.TRANCHE_NEW_LINK + " " + linkNum) != null) { for (String newLink : tagMap.get(TagNames.TRANCHE_NEW_LINK + " " + linkNum)) { boolean foundInMetaData = false; for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(MetaDataAnnotation.PROP_NEW_VERSION)) { if (mda.getValue().equals(newLink)) { foundInMetaData = true; break; } } } if (!foundInMetaData) { md.addAnnotation(new MetaDataAnnotation(MetaDataAnnotation.PROP_NEW_VERSION, newLink)); metaDataChanged = true; addedTag(entry.getId(), hash, MetaDataAnnotation.PROP_NEW_VERSION, newLink, true); } // make sure the new hash's meta data has this as it's old version try { BigHash newHash = BigHash.createHashFromString(newLink); if (!metaDataContainsAnnotation(newHash, MetaDataAnnotation.PROP_OLD_VERSION, hash.toString())) { addMetaDataAnnotationNow(newHash, MetaDataAnnotation.PROP_OLD_VERSION, hash.toString()); } } catch (Exception e) { } } } if (tagMap.get(TagNames.TRANCHE_OLD_LINK + " " + linkNum) != null) { for (String oldLink : tagMap.get(TagNames.TRANCHE_OLD_LINK + " " + linkNum)) { boolean foundInMetaData = false; for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(MetaDataAnnotation.PROP_OLD_VERSION)) { if (mda.getValue().equals(oldLink)) { foundInMetaData = true; break; } } } if (!foundInMetaData) { md.addAnnotation(new MetaDataAnnotation(MetaDataAnnotation.PROP_OLD_VERSION, oldLink)); metaDataChanged = true; addedTag(entry.getId(), hash, MetaDataAnnotation.PROP_OLD_VERSION, oldLink, true); } // make sure the old hash's meta data has this as it's new version try { BigHash oldHash = BigHash.createHashFromString(oldLink); if (!metaDataContainsAnnotation(oldHash, MetaDataAnnotation.PROP_NEW_VERSION, hash.toString())) { addMetaDataAnnotationNow(oldHash, MetaDataAnnotation.PROP_NEW_VERSION, hash.toString()); } } catch (Exception e) { } } } // make sure the meta data annotation for showing meta info is there if it is in the tags if (tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum) != null) { // should only be one entry if (tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).size() > 1) { for (int i = tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).size() - 1; i > 0; i--) { String removedValue = tagMap .get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).remove(i); removedTag(entry.getId(), hash, TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum, removedValue, false); } } // check if it needs to be added to the meta data if (tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).size() == 1 && tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).get(0) .toLowerCase() .equals(MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION .getValue().toLowerCase())) { if (!tagMap.get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).get(0) .toLowerCase() .equals(MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION .getValue().toLowerCase())) { // remove the tag - it's meaningless String removedValue = tagMap .get(TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum).remove(0); removedTag(entry.getId(), hash, TagNames.TRANCHE_SHOW_MD_IF_ENC + " " + linkNum, removedValue, false); } else { // make sure the value is in the meta data boolean inMetaData = false; for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals( MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION .getName()) && mda.getValue().toLowerCase().equals( MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION .getValue().toLowerCase())) { inMetaData = true; } } // add to the meta data? if (!inMetaData) { md.addAnnotation( MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION); metaDataChanged = true; addedTag(entry.getId(), hash, MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION .getName(), MetaDataAnnotation.SHARE_META_DATA_IF_ENCRYPTED_ANNOTATION .getValue(), true); } } } } // set the signatures { String signatures = ""; for (Signature signature : md.getSignatures()) { String signatureStr = signature.getCert().getSubjectDN().getName() .split("CN=")[1].split(",")[0]; // skip adding if already exists if (tagMap.get(TagNames.TRANCHE_SIGNATURES + " " + linkNum) != null) { if (tagMap.get(TagNames.TRANCHE_SIGNATURES + " " + linkNum).get(0) .contains(signatureStr)) { continue; } } signatures = signatures + signatureStr + ", "; } if (signatures.length() != 0) { signatures = signatures.substring(0, signatures.length() - 2); if (tagMap.get(TagNames.TRANCHE_SIGNATURES + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_SIGNATURES + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_SIGNATURES + " " + linkNum).add(signatures); addedTag(entry.getId(), hash, TagNames.TRANCHE_SIGNATURES + " " + linkNum, signatures, false); } else if (!tagMap.get(TagNames.TRANCHE_SIGNATURES + " " + linkNum).get(0) .equals(signatures)) { editedTag(entry.getId(), hash, TagNames.TRANCHE_SIGNATURES + " " + linkNum, tagMap.get(TagNames.TRANCHE_SIGNATURES + " " + linkNum).get(0), signatures, false); tagMap.get(TagNames.TRANCHE_SIGNATURES + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_SIGNATURES + " " + linkNum).add(signatures); } } } // set the timestamps if (tagMap.get(TagNames.TIMESTAMP) == null) { tagMap.put(TagNames.TIMESTAMP, new ArrayList<String>()); tagMap.get(TagNames.TIMESTAMP).add(String.valueOf(md.getTimestamp())); addedTag(entry.getId(), hash, TagNames.TIMESTAMP, String.valueOf(md.getTimestamp()), false); } if (tagMap.get(TagNames.TRANCHE_TIMESTAMP + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_TIMESTAMP + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_TIMESTAMP + " " + linkNum) .add(String.valueOf(md.getTimestamp())); addedTag(entry.getId(), hash, TagNames.TRANCHE_TIMESTAMP + " " + linkNum, String.valueOf(md.getTimestamp()), false); } else if (!tagMap.get(TagNames.TRANCHE_TIMESTAMP + " " + linkNum).get(0) .equals(String.valueOf(md.getTimestamp()))) { editedTag(entry.getId(), hash, TagNames.TRANCHE_TIMESTAMP + " " + linkNum, tagMap.get(TagNames.TRANCHE_TIMESTAMP + " " + linkNum).get(0), String.valueOf(md.getTimestamp()), false); tagMap.get(TagNames.TRANCHE_TIMESTAMP + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_TIMESTAMP + " " + linkNum) .add(String.valueOf(md.getTimestamp())); } // date uploaded String dateUploaded = makeDate(md.getTimestamp()); // if there were no tags, add this date uploaded if (tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum) == null || tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum).size() == 0) { tagMap.put(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum).add(dateUploaded); addedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum, dateUploaded, false); } // if the first date uploaded did not match, set this one as the only one else if (!tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum).get(0) .equals(dateUploaded)) { editedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum, tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum).get(0), dateUploaded, false); tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum).add(dateUploaded); } // if there are more than one date uploaded tags, delete all but the first while (tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum).size() > 1) { String toDelete = tagMap.get(TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum) .remove(1); removedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_UPLOADED + " " + linkNum, toDelete, false); } if (md.isDeleted()) { MetaDataAnnotation deletedAnnotation = null; for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(MetaDataAnnotation.PROP_DELETED)) { try { if (deletedAnnotation == null || Long.valueOf(deletedAnnotation.getValue()) < Long .valueOf(mda.getValue())) { deletedAnnotation = mda; } } catch (Exception e) { } } } if (deletedAnnotation != null) { if (tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_DELETED + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum) .add(deletedAnnotation.getValue()); addedTag(entry.getId(), hash, TagNames.TRANCHE_DELETED + " " + linkNum, deletedAnnotation.getValue(), false); } else { String highestValue = null; for (String value : tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum)) { try { if (highestValue == null || Long.valueOf(value) > Long.valueOf(highestValue)) { highestValue = value; } } catch (Exception e) { } } if (highestValue != null && Long.valueOf(highestValue) < Long .valueOf(deletedAnnotation.getValue())) { tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum) .add(deletedAnnotation.getValue()); addedTag(entry.getId(), hash, TagNames.TRANCHE_DELETED + " " + linkNum, deletedAnnotation.getValue(), false); } // date deleted String dateDeleted = makeDate(Long.valueOf(highestValue)); if (tagMap.get(TagNames.TRANCHE_DATE_DELETED + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_DATE_DELETED + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_DATE_DELETED + " " + linkNum) .add(dateDeleted); addedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_DELETED + " " + linkNum, dateDeleted, false); } else if (!tagMap.get(TagNames.TRANCHE_DATE_DELETED + " " + linkNum).get(0) .equals(dateDeleted)) { editedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_DELETED + " " + linkNum, tagMap.get(TagNames.TRANCHE_DATE_DELETED + " " + linkNum) .get(0), dateDeleted, false); tagMap.get(TagNames.TRANCHE_DATE_DELETED + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_DATE_DELETED + " " + linkNum) .add(dateDeleted); } } } } else { if (tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum) != null) { String highestValue = "0"; for (String value : tagMap.get(TagNames.TRANCHE_DELETED + " " + linkNum)) { if (highestValue == null || Long.valueOf(value) > Long.valueOf(highestValue)) { highestValue = value; } } if (!highestValue.equals("0")) { MetaDataAnnotation mda = new MetaDataAnnotation( MetaDataAnnotation.PROP_DELETED, highestValue); md.addAnnotation(mda); metaDataChanged = true; addedTag(entry.getId(), hash, MetaDataAnnotation.PROP_DELETED, highestValue, true); } } } if (md.isEncrypted()) { // if there are no encrypted tags, add "True" if (tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum) == null || tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).size() == 0) { tagMap.put(TagNames.TRANCHE_ENCRYPTED + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).add("True"); addedTag(entry.getId(), hash, TagNames.TRANCHE_ENCRYPTED + " " + linkNum, "True", false); } // otherwise if the first is not "True", change it else if (!tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).get(0) .equals("True")) { editedTag(entry.getId(), hash, TagNames.TRANCHE_ENCRYPTED + " " + linkNum, tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).get(0), "True", false); tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).add("True"); } // if there's no passphrase tag yet, check the meta data if (tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum) == null || tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum).size() == 0) { // is there a public passphrase? String publicPassphrase = md.getPublicPassphrase(); // no public passphrase in this meta data? if (publicPassphrase == null) { // check for meta data on every server to see if there is a public passphrase set there // possible a lost meta data was recovered that was not available during publishing for (String url : servers) { TrancheServer ts = null; try { ts = IOUtil.connect(url); // get the meta data from the server MetaData mdForPassphrase = MetaDataUtil .read(new ByteArrayInputStream(ts.getMetaData(hash))); // does this md have a passphrase? String passphraseInMD = mdForPassphrase.getPublicPassphrase(); // got one! break the loop if (passphraseInMD != null) { publicPassphrase = passphraseInMD; break; } } catch (Exception e) { e.printStackTrace(); } finally { IOUtil.safeClose(ts); } } } // if still nothing, try to get the passphrase from the passphrases database if (publicPassphrase == null) { // need to go to the private passphrases to download the project file if (Passphrases.getPassphrase(hash) != null) { gft.setPassphrase(Passphrases.getPassphrase(hash)); log.println("Set passphrase from private passphrase db"); } else { log.println( "No public or private passphrase found - cannot get project information."); } } else { log.println("Public passphrase set from the meta data."); tagMap.put(TagNames.TRANCHE_PASSPHRASE + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum) .add(publicPassphrase); addedTag(entry.getId(), hash, TagNames.TRANCHE_PASSPHRASE + " " + linkNum, publicPassphrase, false); } } // the passphrase could have been set if it was found in the meta data if (tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum) != null && !tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum).isEmpty()) { // remove all but the one of the passphrases - should never be more than one if (tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum).size() > 1) { log.println( "More than one passphrase tag found - removing all but the first."); // delete all but the first while (tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum).size() > 1) { String toDelete = tagMap .get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum).remove(1); removedTag(entry.getId(), hash, TagNames.TRANCHE_PASSPHRASE + " " + linkNum, toDelete, false); } } // determine which public passphrase to use String publicPassphrase = null; // if the published passphrase and the tags passphrase are different, use the most recent if (md.isPublicPassphraseSet() && !tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum).get(0) .equals(md.getPublicPassphrase())) { // get the date from the tags String dateTags = null; if (tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) != null && !tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) .isEmpty()) { dateTags = tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) .get(0); } // get the date from the meta data String dateMetaData = null; for (MetaDataAnnotation mda : md.getAnnotations()) { try { if (mda.getName().equals(mda.PROP_PUBLISHED_TIMESTAMP)) { dateMetaData = makeDate(Long.valueOf(mda.getValue())); } } catch (Exception e) { } } boolean useTags = true; if (dateMetaData != null && dateTags == null) { useTags = false; } else if (dateMetaData != null && dateTags != null) { if (dateMetaData.compareTo(dateTags) >= 0) { useTags = false; } } if (useTags) { publicPassphrase = tagMap .get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum).get(0); } else { // change the tags to be the same as the md public passphrase String toDelete = tagMap .get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum).remove(0); removedTag(entry.getId(), hash, TagNames.TRANCHE_PASSPHRASE + " " + linkNum, toDelete, false); tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum) .add(md.getPublicPassphrase()); addedTag(entry.getId(), hash, TagNames.TRANCHE_PASSPHRASE + " " + linkNum, md.getPublicPassphrase(), false); publicPassphrase = md.getPublicPassphrase(); } } else { publicPassphrase = tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum) .get(0); } if (publicPassphrase != null) { // just go ahead and publish the passphras to all meta data no matter what // in the future, need to check all meta data to see if it's necessary to republish the passphrase // set the gft passphrase gft.setPassphrase(publicPassphrase); // set the passphrase in the meta data md.setPublicPassphrase(publicPassphrase); metaDataChanged = true; } } // how many published passphrase annotations are there? int publishedMetaDataAnnotationCount = 0; for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP)) { publishedMetaDataAnnotationCount++; } } // if need to determine date published if (md.getPublicPassphrase() == null) { // make sure there are no published meta annotations while (publishedMetaDataAnnotationCount > 0) { for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP)) { removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); publishedMetaDataAnnotationCount--; break; } } } // set the published tag to "Unknown" if (tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) == null || tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) .size() == 0) { tagMap.put(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).add("Unknown"); addedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, "Unknown", false); } else if (!tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).get(0) .equals("Unknown")) { editedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).get(0), "Unknown", false); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).add("Unknown"); } } else { // are there no published annotations in the md? very odd if (publishedMetaDataAnnotationCount == 0) { // add a published meta data annotation with the current timestamp String timestampStr = String.valueOf(System.currentTimeMillis()); addedTag(entry.getId(), hash, MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP, timestampStr, true); metaDataChanged = true; md.getAnnotations().add(new MetaDataAnnotation( MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP, timestampStr)); publishedMetaDataAnnotationCount++; } else { // remove all but one of the published timestamps while (publishedMetaDataAnnotationCount > 1) { for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName() .equals(MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP)) { removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); publishedMetaDataAnnotationCount--; break; } } } // get the date published String datePublished = null; for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP)) { // exception thrown if value is not a timestamp try { datePublished = makeDate(Long.valueOf(mda.getValue())); } catch (Exception e) { // delete this annotation removedTag(entry.getId(), hash, MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP, mda.getValue(), true); metaDataChanged = true; md.getAnnotations().remove(mda); publishedMetaDataAnnotationCount--; } finally { break; } } } // if the date published was bad - had to remove it, so put in a new one if (datePublished == null) { // set the new date published to now long timestamp = System.currentTimeMillis(); String timestampStr = String.valueOf(timestamp); datePublished = makeDate(timestamp); // add the date published annotation to the meta data if (publishedMetaDataAnnotationCount == 0) { // add a meta data annotation addedTag(entry.getId(), hash, MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP, datePublished, true); metaDataChanged = true; md.getAnnotations() .add(new MetaDataAnnotation( MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP, datePublished)); publishedMetaDataAnnotationCount++; } } // if no date published tag, add it if (tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) == null || tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) .size() == 0) { tagMap.put(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) .add(datePublished); addedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, datePublished, false); } // if our date published annotation is wrong, reset it else if (!tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).get(0) .equals(datePublished)) { editedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) .get(0), datePublished, false); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) .add(datePublished); } } } } else { // if there are no encrypted tags, add "False" if (tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum) == null || tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).size() == 0) { tagMap.put(TagNames.TRANCHE_ENCRYPTED + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).add("False"); addedTag(entry.getId(), hash, TagNames.TRANCHE_ENCRYPTED + " " + linkNum, "False", false); } // otherwise if the first is not "False", change it else if (!tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).get(0) .equals("False")) { editedTag(entry.getId(), hash, TagNames.TRANCHE_ENCRYPTED + " " + linkNum, tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).get(0), "False", false); tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).add("False"); } // remove any passphrase tags if they exist if (tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum) != null) { for (String toDelete : tagMap .get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum)) { if (tagMap.get(TagNames.TRANCHE_PASSPHRASE + " " + linkNum) .remove(toDelete)) { removedTag(entry.getId(), hash, TagNames.TRANCHE_PASSPHRASE + " " + linkNum, toDelete, false); } } // remove the arraylist so there is no confusion tagMap.remove(TagNames.TRANCHE_PASSPHRASE + " " + linkNum); } // the date published equals to the meta data timestamp // if there were no tags, add this date uploaded as the date published if (tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) == null || tagMap .get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).size() == 0) { tagMap.put(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).add(dateUploaded); addedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, dateUploaded, false); } // if the first date published did not match the date uploaded, set this one as the only one else if (!tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).get(0) .equals(dateUploaded)) { editedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).get(0), dateUploaded, false); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).add(dateUploaded); } } // if there are more than one encrypted tags, delete all but the first if (tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum) != null) { while (tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum).size() > 1) { String toDelete = tagMap.get(TagNames.TRANCHE_ENCRYPTED + " " + linkNum) .remove(1); removedTag(entry.getId(), hash, TagNames.TRANCHE_ENCRYPTED + " " + linkNum, toDelete, false); } } // if there are more than one date published tags, delete all but the first if (tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) != null) { while (tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum).size() > 1) { String toDelete = tagMap.get(TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum) .remove(1); removedTag(entry.getId(), hash, TagNames.TRANCHE_DATE_PUBLISHED + " " + linkNum, toDelete, false); } } // get the project file if (pf == null && md.isProjectFile()) { try { if (md != null && md.isProjectFile()) { // increment the chunk meta count by the size of the project file chunkAndMetaCount += md.getParts().size(); File tempFile = TempFileUtil.createTemporaryFile(); try { // catch invalid downloads, throw otherwise try { gft.getFile(tempFile); } catch (CantVerifySignatureException e) { addInvalid(entry.getId(), hash.toString(), tagMap, linkNum, e); log.println("ERROR: Downloaded project file is invalid."); } catch (Exception e) { if (e.getMessage() != null && (e.getMessage().toLowerCase() .contains("invalid") || e.getMessage().toLowerCase().contains("validate") || e.getMessage().toLowerCase().contains( "Decoded file does not match the expected file!"))) { addInvalid(entry.getId(), hash.toString(), tagMap, linkNum, e); log.println("ERROR: Project file invalid."); } else { err.println(e.getMessage()); throw e; } } // treat it as if it is a project file FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(tempFile); bis = new BufferedInputStream(fis); pf = ProjectFileUtil.read(bis); } catch (Exception e) { log.println("ERROR: Project file invalid."); addInvalid(entry.getId(), hash.toString(), tagMap, linkNum, e); bis.close(); fis.close(); } } finally { try { tempFile.delete(); } catch (Exception e) { err.println(e.getMessage()); } } } } catch (Exception e) { log.println("ERROR: Could not get project file"); err.println(e.getMessage()); } } if (pf != null && md.isProjectFile()) { // go through all of the files getting the file type and size try { for (ProjectFilePart pfp : pf.getParts()) { try { // update the number of meta data and chunks count // meta data chunkAndMetaCount++; // # chunks = ceiling of the size of the file divided by one MB chunkAndMetaCount += Math .ceil(Double.valueOf(pfp.getPaddingAdjustedLength()) / Double.valueOf(DataBlockUtil.ONE_MB)); // read the name and the size String name = pfp.getRelativeName().trim().toLowerCase(); if (name.contains("/")) { name = name.substring(name.lastIndexOf('/') + 1); } if (!name.contains(".")) { continue; } long size = pfp.getPaddingAdjustedLength(); // parse the file type(s) //while (name.contains(".")) { name = name.substring(name.lastIndexOf(".") + 1); // create the keys if there are none if (!numFilesFileTypeMap.containsKey(name)) { numFilesFileTypeMap.put(name, BigInteger.ZERO); } if (!sizeFileTypeMap.containsKey(name)) { sizeFileTypeMap.put(name, BigInteger.ZERO); } // increment the values appropriately numFilesFileTypeMap.put(name, numFilesFileTypeMap.get(name).add(BigInteger.ONE)); sizeFileTypeMap.put(name, sizeFileTypeMap.get(name).add(BigInteger.valueOf(size))); // } } catch (Exception e) { log.println("ERROR: Problem reading a file's information."); err.println(e.getMessage()); } } printFileTypeLog(); } catch (Exception e) { log.println("ERROR: Problem reading file type information."); err.println(e.getMessage()); } // set the files no matter what if (tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_FILES + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum) .add(String.valueOf(pf.getParts().size())); addedTag(entry.getId(), hash, TagNames.TRANCHE_FILES + " " + linkNum, String.valueOf(pf.getParts().size()), false); } else if (!tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum).get(0) .equals(String.valueOf(pf.getParts().size()))) { editedTag(entry.getId(), hash, TagNames.TRANCHE_FILES + " " + linkNum, tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum).get(0), String.valueOf(pf.getParts().size()), false); tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum) .add(String.valueOf(pf.getParts().size())); } // set the size no matter what if (tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_SIZE + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum).add(pf.getSize().toString()); addedTag(entry.getId(), hash, TagNames.TRANCHE_SIZE + " " + linkNum, pf.getSize().toString(), false); } else if (!tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum).get(0) .equals(pf.getSize().toString())) { editedTag(entry.getId(), hash, TagNames.TRANCHE_SIZE + " " + linkNum, tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum).get(0), pf.getSize().toString(), false); tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum).add(pf.getSize().toString()); } // title if (tagMap.get(TagNames.TITLE) == null && pf.getName() != null) { tagMap.put(TagNames.TITLE, new ArrayList<String>()); tagMap.get(TagNames.TITLE).add(pf.getName()); addedTag(entry.getId(), hash, TagNames.TITLE, pf.getName(), false); } // tranche link name // only have a tranche link name if it is different than the title if (tagMap.get(TagNames.TRANCHE_LINK_NAME + " " + linkNum) != null && pf.getName() != null && tagMap.get(TagNames.TRANCHE_LINK_NAME + " " + linkNum).get(0) .equals(tagMap.get(TagNames.TITLE).get(0))) { for (String value : tagMap.get(TagNames.TRANCHE_LINK_NAME + " " + linkNum)) { removedTag(entry.getId(), hash, TagNames.TRANCHE_LINK_NAME + " " + linkNum, value, false); } tagMap.remove(TagNames.TRANCHE_LINK_NAME + " " + linkNum); } // description if (tagMap.get(TagNames.DESCRIPTION) == null && pf.getDescription() != null) { tagMap.put(TagNames.DESCRIPTION, new ArrayList<String>()); tagMap.get(TagNames.DESCRIPTION).add(pf.getDescription()); addedTag(entry.getId(), hash, TagNames.DESCRIPTION, pf.getDescription(), false); } // tranche description // only have a tranche link if it is different from the entry description if (tagMap.get(TagNames.TRANCHE_DESCRIPTION + " " + linkNum) != null && pf.getDescription() != null && tagMap.get(TagNames.TRANCHE_DESCRIPTION + " " + linkNum).get(0) .equals(tagMap.get(TagNames.DESCRIPTION).get(0))) { for (String value : tagMap.get(TagNames.TRANCHE_DESCRIPTION + " " + linkNum)) { removedTag(entry.getId(), hash, TagNames.TRANCHE_DESCRIPTION + " " + linkNum, value, false); } tagMap.remove(TagNames.TRANCHE_DESCRIPTION + " " + linkNum); } } if (!md.isProjectFile()) { // # chunks = ceiling of the size of the file divided by one MB chunkAndMetaCount += md.getParts().size(); // set the files no matter what if (tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_FILES + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum).add("1"); addedTag(entry.getId(), hash, TagNames.TRANCHE_FILES + " " + linkNum, "1", false); } else if (!tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum).get(0).equals("1")) { editedTag(entry.getId(), hash, TagNames.TRANCHE_FILES + " " + linkNum, tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum).get(0), "1", false); tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_FILES + " " + linkNum).add("1"); } // set the size no matter what if (tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum) == null) { tagMap.put(TagNames.TRANCHE_SIZE + " " + linkNum, new ArrayList<String>()); tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum) .add(String.valueOf(hash.getLength())); addedTag(entry.getId(), hash, TagNames.TRANCHE_SIZE + " " + linkNum, String.valueOf(hash.getLength()), false); } else if (!tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum).get(0) .equals(String.valueOf(hash.getLength()))) { editedTag(entry.getId(), hash, TagNames.TRANCHE_SIZE + " " + linkNum, tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum).get(0), String.valueOf(hash.getLength()), false); tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum).clear(); tagMap.get(TagNames.TRANCHE_SIZE + " " + linkNum) .add(String.valueOf(hash.getLength())); } // add the title if (md.getName() != null) { if (tagMap.get(TagNames.TITLE) == null) { tagMap.put(TagNames.TITLE, new ArrayList<String>()); tagMap.get(TagNames.TITLE).add(md.getName()); addedTag(entry.getId(), hash, TagNames.TITLE, md.getName(), false); } // check if the tranche link name is different from the title if (tagMap.get(TagNames.TRANCHE_LINK_NAME + " " + linkNum) != null && tagMap.get(TagNames.TRANCHE_LINK_NAME + " " + linkNum).get(0) .equals(tagMap.get(TagNames.TITLE).get(0))) { for (String value : tagMap .get(TagNames.TRANCHE_LINK_NAME + " " + linkNum)) { removedTag(entry.getId(), hash, TagNames.TRANCHE_LINK_NAME + " " + linkNum, value, false); } tagMap.remove(TagNames.TRANCHE_LINK_NAME + " " + linkNum); } } } } // just go ahead and possibly overwrite all older values of TagNames.HAS_DATA if (tagMap.get(TagNames.HAS_DATA) == null) { tagMap.put(TagNames.HAS_DATA, new ArrayList<String>()); tagMap.get(TagNames.HAS_DATA).add("Yes"); addedTag(entry.getId(), hash, TagNames.HAS_DATA, "Yes", false); } else if (!tagMap.get(TagNames.HAS_DATA).get(0).equals("Yes")) { editedTag(entry.getId(), hash, TagNames.HAS_DATA, tagMap.get(TagNames.HAS_DATA).get(0), "Yes", false); tagMap.get(TagNames.HAS_DATA).clear(); tagMap.get(TagNames.HAS_DATA).add("Yes"); } if (tagMap.get(TagNames.TYPE) == null) { tagMap.put(TagNames.TYPE, new ArrayList<String>()); tagMap.get(TagNames.TYPE).add("Data"); addedTag(entry.getId(), hash, TagNames.TYPE, "Data", false); } if (metaDataChanged && makeChanges) { log.println("Publishing changes to the meta data."); // create the bytestream ByteArrayOutputStream baos = new ByteArrayOutputStream(); // turn the metaData into a byte stream MetaDataUtil.write(md, baos); for (String url : servers) { try { // connect TrancheServer ts = IOUtil.connect(url); try { if (ts.hasMetaData(hash)) { // upload the changes - try up to 3 times Exception ex = null; for (int i = 0; i < 3; i++) { try { IOUtil.setMetaData(ts, user.getCertificate(), user.getPrivateKey(), hash, baos.toByteArray()); log.println("Set meta data to " + url); ex = null; break; } catch (Exception e) { ex = e; continue; } } if (ex != null) { throw ex; } } } finally { IOUtil.safeClose(ts); } } catch (Exception e) { err.println(e.getMessage()); log.println("ERROR: Could not set meta data to " + url); } } log.println("Done publishing meta data"); } } catch (Exception e) { log.println("ERROR: A problem occurred while editing the entry"); err.println(e.getMessage()); } // update the database saveToDatabase(entry.getId(), tagMap); } } } catch (Exception e) { err.println(e.getMessage()); } log.println("Checking if there is any new data on the network."); try { for (BigHash hash : hashesOnNetwork) { // do not add this data to the tags if it already exists in there if (hashesInTags.contains(hash)) { continue; } // add this data to the tags db try { // reset the meta data and the project file MetaData md = null; ProjectFile pf = null; // set up the getfiletool GetFileTool gft = new GetFileTool(); gft.setValidate(validate); gft.setHash(hash); // get the meta data try { md = gft.getMetaData(); } catch (CantVerifySignatureException e) { addInvalid(-1, hash.toString(), "", "", "", "", e); log.println("ERROR: Downloaded meta data is invalid."); } catch (Exception e) { if (e.getMessage() != null && e.getMessage().toLowerCase().contains("can't find metadata")) { // note that this meta data is missing from the network addMissing(-1, hash.toString(), "", "", "", ""); log.println("ERROR: Could not get meta data."); continue; } else { addInvalid(-1, hash.toString(), "", "", "", "", e); log.println("ERROR: Downloaded meta data is invalid."); continue; } } // if the tag entry doesnt exists, create a new one long entryId = -1; if (makeChanges) { entryId = Database.createEntry(); } // add all of the info as tags if (makeChanges) { Database.addTag(entryId, TagNames.HAS_DATA, "Yes"); } addedTag(entryId, hash, TagNames.HAS_DATA, "Yes", false, false); if (makeChanges) { Database.addTag(entryId, TagNames.TYPE, "Data"); } addedTag(entryId, hash, TagNames.TYPE, "Data", false, false); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_LINK + " 1", hash.toString()); } addedTag(entryId, hash, TagNames.TRANCHE_LINK + " 1", hash.toString(), false, false); if (makeChanges) { Database.addTag(entryId, TagNames.TIMESTAMP, String.valueOf(md.getTimestamp())); } addedTag(entryId, hash, TagNames.TIMESTAMP, String.valueOf(md.getTimestamp()), false, false); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_TIMESTAMP + " 1", String.valueOf(md.getTimestamp())); } addedTag(entryId, hash, TagNames.TRANCHE_TIMESTAMP + " 1", String.valueOf(md.getTimestamp()), false, false); String datePublished = null; if (md != null) { // set the new/old version for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(MetaDataAnnotation.PROP_DELETE_NEW_VERSION)) { try { BigHash newVersionHash = BigHash.createHashFromString(mda.getValue()); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_DELETE_NEW_LINK + " 1", newVersionHash.toString()); } addedTag(entryId, hash, TagNames.TRANCHE_DELETE_NEW_LINK + " 1", newVersionHash.toString(), false, false); } catch (Exception e) { } } else if (mda.getName().equals(MetaDataAnnotation.PROP_DELETE_OLD_VERSION)) { try { BigHash oldVersionHash = BigHash.createHashFromString(mda.getValue()); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_DELETE_OLD_LINK + " 1", oldVersionHash.toString()); } addedTag(entryId, hash, TagNames.TRANCHE_DELETE_OLD_LINK + " 1", oldVersionHash.toString(), false, false); } catch (Exception e) { } } else if (mda.getName().equals(MetaDataAnnotation.PROP_UNDELETED)) { if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_UNDELETED + " 1", mda.getValue()); } addedTag(entryId, hash, TagNames.TRANCHE_UNDELETED + " 1", mda.getValue(), false, false); } else if (mda.getName().equals(MetaDataAnnotation.PROP_NEW_VERSION)) { try { BigHash newVersionHash = BigHash.createHashFromString(mda.getValue()); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_NEW_LINK + " 1", newVersionHash.toString()); } addedTag(entryId, hash, TagNames.TRANCHE_NEW_LINK + " 1", newVersionHash.toString(), false, false); } catch (Exception e) { } } else if (mda.getName().equals(MetaDataAnnotation.PROP_OLD_VERSION)) { try { BigHash oldVersionHash = BigHash.createHashFromString(mda.getValue()); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_OLD_LINK + " 1", oldVersionHash.toString()); } addedTag(entryId, hash, TagNames.TRANCHE_OLD_LINK + " 1", oldVersionHash.toString(), false, false); } catch (Exception e) { } } else if (mda.getName().equals(MetaDataAnnotation.PROP_PUBLISHED_TIMESTAMP)) { try { // date published datePublished = makeDate(Long.valueOf(mda.getValue())); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_DATE_PUBLISHED + " 1", datePublished); } addedTag(entryId, hash, TagNames.TRANCHE_DATE_PUBLISHED + " 1", datePublished, false, false); } catch (Exception e) { } } } String dateUploaded = makeDate(md.getTimestamp()); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_DATE_UPLOADED + " 1", dateUploaded); } addedTag(entryId, hash, TagNames.TRANCHE_DATE_UPLOADED + " 1", dateUploaded, false, false); if (md.isEncrypted()) { if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_ENCRYPTED + " 1", "True"); } addedTag(entryId, hash, TagNames.TRANCHE_ENCRYPTED + " 1", "True", false, false); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_DATE_PUBLISHED + " 1", "Unknown"); } addedTag(entryId, hash, TagNames.TRANCHE_DATE_PUBLISHED + " 1", "Unknown", false, false); try { String passphrase = Passphrases.getPassphrase(hash); if (passphrase != null) { gft.setPassphrase(passphrase); log.println("Set the passphrase from the private passphrase db."); } } catch (Exception e) { } } else { if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_ENCRYPTED + " 1", "False"); } addedTag(entryId, hash, TagNames.TRANCHE_ENCRYPTED + " 1", "False", false, false); if (datePublished == null) { if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_DATE_PUBLISHED + " 1", dateUploaded); } addedTag(entryId, hash, TagNames.TRANCHE_DATE_PUBLISHED + " 1", dateUploaded, false, false); } } if (md.isDeleted()) { MetaDataAnnotation deletedAnnotation = null; for (MetaDataAnnotation mda : md.getAnnotations()) { if (mda.getName().equals(MetaDataAnnotation.PROP_DELETED)) { try { if (deletedAnnotation == null || Long.valueOf(deletedAnnotation.getValue()) < Long .valueOf(mda.getValue())) { deletedAnnotation = mda; } } catch (Exception e) { } } } if (deletedAnnotation != null) { if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_DELETED + " 1", deletedAnnotation.getValue()); } addedTag(entryId, hash, TagNames.TRANCHE_DELETED + " 1", deletedAnnotation.getValue(), false, false); // date deleted try { String dateDeleted = makeDate(Long.valueOf(deletedAnnotation.getValue())); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_DATE_DELETED + " 1", dateDeleted); } addedTag(entryId, hash, TagNames.TRANCHE_DATE_DELETED + " 1", dateDeleted, false, false); } catch (Exception e) { } } } String signatures = ""; for (Signature signature : md.getSignatures()) { String signatureStr = signature.getCert().getSubjectDN().getName().split("CN=")[1] .split(",")[0]; signatures = signatures + signatureStr + ", "; } if (signatures.length() != 0) { signatures = signatures.substring(0, signatures.length() - 2); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_SIGNATURES + " 1", signatures); } addedTag(entryId, hash, TagNames.TRANCHE_SIGNATURES + " 1", signatures, false, false); } if (md.isProjectFile()) { try { if (md != null && md.isProjectFile()) { File tempFile = TempFileUtil.createTemporaryFile(); try { // catch invalid downloads, throw otherwise try { gft.getFile(tempFile); } catch (CantVerifySignatureException e) { addInvalid(entryId, hash.toString(), "", "", "", "", e); log.println("ERROR: Downloaded project file is invalid."); } catch (Exception e) { if (e.getMessage() != null && (e.getMessage().toLowerCase() .contains("validate") || e.getMessage().toLowerCase().contains( "Decoded file does not match the expected file!"))) { addInvalid(entryId, hash.toString(), "", "", "", "", e); log.println("ERROR: Project file invalid."); } else { throw e; } } // treat it as if it is a project file FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(tempFile); bis = new BufferedInputStream(fis); pf = ProjectFileUtil.read(bis); } catch (Exception e) { log.println("ERROR: Project file invalid."); addInvalid(entryId, hash.toString(), "", "", "", "", e); bis.close(); fis.close(); } } finally { try { tempFile.delete(); } catch (Exception e) { err.println(e.getMessage()); } } } if (pf != null) { if (pf.getName() != null) { if (makeChanges) { Database.addTag(entryId, TagNames.TITLE, pf.getName()); } addedTag(entryId, hash, TagNames.TITLE, pf.getName(), false, false); } if (pf.getDescription() != null) { if (makeChanges) { Database.addTag(entryId, TagNames.DESCRIPTION, pf.getDescription()); } addedTag(entryId, hash, TagNames.DESCRIPTION, pf.getDescription(), false, false); } if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_SIZE + " 1", pf.getSize().toString()); } addedTag(entryId, hash, TagNames.TRANCHE_SIZE + " 1", pf.getSize().toString(), false, false); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_FILES + " 1", String.valueOf(pf.getParts().size())); } addedTag(entryId, hash, TagNames.TRANCHE_FILES + " 1", String.valueOf(pf.getParts().size()), false, false); } } catch (Exception e) { log.println("ERROR: Could not load the project file."); err.println(e.getMessage()); } } else { if (makeChanges) { Database.addTag(entryId, TagNames.TITLE, md.getName()); } addedTag(entryId, hash, TagNames.TITLE, md.getName(), false, false); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_FILES + " 1", "1"); } addedTag(entryId, hash, TagNames.TRANCHE_FILES + " 1", "1", false, false); if (makeChanges) { Database.addTag(entryId, TagNames.TRANCHE_SIZE + " 1", String.valueOf(hash.getLength())); } addedTag(entryId, hash, TagNames.TRANCHE_SIZE + " 1", String.valueOf(hash.getLength()), false, false); } } addedEntries.add(entryId); hashesInTags.add(hash); } catch (Exception e) { log.println("ERROR: There was a problem adding the new entry"); err.println(e.getMessage()); } } } catch (Exception e) { log.println("ERROR: There was a problem checking for new data on the network."); err.println(e.getMessage()); } log.println("Finished updating the tags database"); }
From source file:org.alfresco.opencmis.CMISTest.java
/** * MNT-8804 related test :/*from www . j a v a 2 s. c o m*/ * Check CMISConnector.query for search nodes in environment with corrupted indexes */ @Test public void testQueryNodesWithCorruptedIndexes() { AuthenticationUtil.pushAuthentication(); AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); final String TEST_NAME = "mnt8804test-"; final String docName = TEST_NAME + GUID.generate(); /* Create node */ final FileInfo document = transactionService.getRetryingTransactionHelper() .doInTransaction(new RetryingTransactionCallback<FileInfo>() { @Override public FileInfo execute() throws Throwable { NodeRef companyHomeNodeRef = repositoryHelper.getCompanyHome(); /* Create folder within companyHome */ String folderName = TEST_NAME + GUID.generate(); FileInfo folderInfo = fileFolderService.create(companyHomeNodeRef, folderName, ContentModel.TYPE_FOLDER); nodeService.setProperty(folderInfo.getNodeRef(), ContentModel.PROP_NAME, folderName); assertNotNull(folderInfo); /* Create document to query */ FileInfo document = fileFolderService.create(folderInfo.getNodeRef(), docName, ContentModel.TYPE_CONTENT); assertNotNull(document); nodeService.setProperty(document.getNodeRef(), ContentModel.PROP_NAME, docName); return document; } }); final Pair<Long, NodeRef> nodePair = nodeDAO.getNodePair(document.getNodeRef()); /* delete node's metadata directly */ transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() { @Override public Void execute() throws Throwable { Pair<Long, ChildAssociationRef> childAssocPair = nodeDAO.getPrimaryParentAssoc(nodePair.getFirst()); nodeDAO.deleteChildAssoc(childAssocPair.getFirst()); nodeDAO.deleteNode(nodePair.getFirst()); return null; } }); /* ensure the node does not exist */ assertTrue(!nodeService.exists(nodePair.getSecond())); String queryString = "SELECT * FROM cmis:document WHERE cmis:name='" + docName + "'"; ObjectList resultList = cmisConnector.query(queryString, Boolean.FALSE, IncludeRelationships.NONE, "cmis:none", BigInteger.ONE, BigInteger.ZERO); assertEquals(resultList.getNumItems(), BigInteger.ZERO); // prepare cmis query CMISQueryOptions options = new CMISQueryOptions(queryString, cmisConnector.getRootStoreRef()); CmisVersion cmisVersion = cmisConnector.getRequestCmisVersion(); options.setCmisVersion(cmisVersion); options.setQueryMode(CMISQueryMode.CMS_WITH_ALFRESCO_EXTENSIONS); options.setSkipCount(0); options.setMaxItems(100); /* make query bypassing CMISConnector */ org.alfresco.opencmis.search.CMISResultSet rs = cmisConnector.getOpenCMISQueryService().query(options); assertEquals(rs.getNumberFound(), 0); }