/** * Get the TdbTitle name from the properties. Fall back to a name derived from the TdbAU name if * not specified. * * @param props a group of properties * @param au the TdbAu * @return a TdbTitle name */ private String getTdbTitleName(Properties props, TdbAu au) { // use "journalTitle" prop if specified, or synthesize it // from auName and one of several properties String titleName = getTdbTitleName(props); if (titleName == null) { String year = au.getYear(); String volume = au.getVolume(); String issue = au.getIssue(); String auName = au.getName(); String auNameLC = auName.toLowerCase(); if ((volume != null) && auNameLC.endsWith(" vol " + volume)) { titleName = auName.substring(0, auName.length() - " vol ".length() - volume.length()); } else if ((volume != null) && auNameLC.endsWith(" volume " + volume)) { titleName = auName.substring(0, auName.length() - " volume ".length() - volume.length()); } else if ((issue != null) && auNameLC.endsWith(" issue " + issue)) { titleName = auName.substring(0, auName.length() - " issue ".length() - issue.length()); } else if ((year != null) && auNameLC.endsWith(" " + year)) { titleName = auName.substring(0, auName.length() - " ".length() - year.length()); } else { titleName = UNKNOWN_TITLE_PREFIX + "[" + auName + "]"; } } return titleName; }
/** * Find existing TdbAu with same Id as another one. * * @param otherAu another TdbAu * @return an existing TdbAu already in thisTdb */ protected TdbAu findExistingTdbAu(TdbAu otherAu) { // check for duplicate AU with same plugin for this Tdb Collection<TdbAu.Id> auIds = getTdbAuIds(otherAu.getPluginId()); for (TdbAu.Id auId : auIds) { if (auId.equals(otherAu.getId())) { return auId.getTdbAu(); } } return null; }
/** * Unregister the au with this Tdb for its plugin. * * @param au the TdbAu * @return <code>false</code> if au was not registered, otherwise <code>true</code> */ private boolean removeTdbAuForPlugin(TdbAu au) { // if can't add au to title, we need to undo the au // registration and re-throw the exception we just caught String pluginId = au.getPluginId(); Collection<TdbAu.Id> c = pluginIdTdbAuIdsMap.get(pluginId); if (c.remove(au.getId())) { if (c.isEmpty()) { pluginIdTdbAuIdsMap.remove(c); } tdbAuCount--; return true; } return false; }
/** * Register the au with this Tdb for its plugin. * * @param au the TdbAu * @return <code>false</code> if already registered, otherwise <code>true</code> */ private boolean addTdbAuForPlugin(TdbAu au) { // add AU to list for plugins String pluginId = au.getPluginId(); Collection<TdbAu.Id> auids = pluginIdTdbAuIdsMap.get(pluginId); if (auids == null) { auids = new HashSet<TdbAu.Id>(); pluginIdTdbAuIdsMap.put(pluginId, auids); } if (!auids.add(au.getId())) { return false; } // increment the total AU count; tdbAuCount++; return true; }
/** * Add a new TdbAu to this title database. The TdbAu must have its pluginID, and title set. The * TdbAu''s title must also have its titleId and publisher set. The publisher name must be unique * to all publishers in this Tdb. * * @param au the TdbAu to add. * @throws TdbException if Tdb is sealed, this is a duplicate au, or the au's publisher is a * duplicate */ public void addTdbAu(TdbAu au) throws TdbException { if (au == null) { throw new IllegalArgumentException("TdbAu cannot be null"); } // verify not sealed if (isSealed()) { throw new TdbException("Cannot add TdbAu to sealed Tdb"); } // validate title TdbTitle title = au.getTdbTitle(); if (title == null) { throw new IllegalArgumentException("TdbAu's title not set"); } // validate publisher TdbPublisher publisher = title.getTdbPublisher(); if (publisher == null) { throw new IllegalArgumentException("TdbAu's publisher not set"); } // make sure publisher is not a duplicate String pubName = publisher.getName(); TdbPublisher oldPublisher = tdbPublisherMap.put(pubName, publisher); if ((oldPublisher != null) && (oldPublisher != publisher)) { // restore old publisher and report error tdbPublisherMap.put(pubName, oldPublisher); throw new TdbException("New au publisher with duplicate name: " + pubName); } // register the au with this instance if (!addTdbAuForPlugin(au)) { // remove new publisher and report error if (oldPublisher == null) { tdbPublisherMap.remove(pubName); } throw new TdbException("Cannot register au " + au.getName()); } }
/** * Get or create TdbTitle for the specified properties and TdbAu. * * @param props the properties * @param au the TdbAu * @return the corresponding TdbTitle */ private TdbTitle getTdbTitle(Properties props, TdbAu au) { TdbTitle title = null; // get publisher name String publisherNameFromProps = getTdbPublisherName(props, au); // get the title name String titleNameFromProps = getTdbTitleName(props, au); // get the title ID String titleIdFromProps = getTdbTitleId(props, au); String titleId = titleIdFromProps; if (titleId == null) { // generate a titleId if one not specified, using the // hash code of the combined title name and publisher names int hash = (titleNameFromProps + publisherNameFromProps).hashCode(); titleId = (hash < 0) ? ("id:1" + (-hash)) : ("id:0" + hash); } // get publisher specified by property name TdbPublisher publisher = tdbPublisherMap.get(publisherNameFromProps); if (publisher != null) { // find title from publisher title = publisher.getTdbTitleById(titleId); if (title != null) { // warn that title name is different if (!title.getName().equals(titleNameFromProps)) { logger.warning( "Title for au \"" + au.getName() + "\": \"" + titleNameFromProps + "\" is different than existing title \"" + title.getName() + "\" for id " + titleId + " -- using existing title."); } return title; } } if (publisher == null) { // warn of missing publisher name if (publisherNameFromProps.startsWith(UNKNOWN_PUBLISHER_PREFIX)) { logger.warning( "Publisher missing for au \"" + au.getName() + "\" -- using \"" + publisherNameFromProps + "\""); } // create new publisher for specified publisher name publisher = new TdbPublisher(publisherNameFromProps); tdbPublisherMap.put(publisherNameFromProps, publisher); } // warn of missing title name and/or id if (titleNameFromProps.startsWith(UNKNOWN_TITLE_PREFIX)) { logger.warning( "Title missing for au \"" + au.getName() + "\" -- using \"" + titleNameFromProps + "\""); } if (titleIdFromProps == null) { logger.debug2("Title ID missing for au \"" + au.getName() + "\" -- using " + titleId); } // create title and add to publisher title = new TdbTitle(titleNameFromProps, titleId); try { publisher.addTdbTitle(title); } catch (TdbException ex) { // shouldn't happen: title already exists in publisher logger.error(ex.getMessage(), ex); } return title; }
/** * Add a TdbAu to a TdbTitle and TdbPubisher, and add links to the TdbTitle specified by the * properties. * * @param props the properties * @param au the TdbAu to add * @throws TdbException if the AU already exists in this Tdb */ private void addTdbAu(Properties props, TdbAu au) throws TdbException { // add au for plugin assuming it is not a duplicate if (!addTdbAuForPlugin(au)) { // au already registered -- report existing au TdbAu existingAu = findExistingTdbAu(au); String titleName = getTdbTitleName(props, au); if (!titleName.equals(existingAu.getTdbTitle().getName())) { throw new TdbException( "Cannot add duplicate au entry: \"" + au.getName() + "\" for title \"" + titleName + "\" with same definition as existing au entry: \"" + existingAu.getName() + "\" for title \"" + existingAu.getTdbTitle().getName() + "\" to title database"); } else if (!existingAu.getName().equals(au.getName())) { // error because it could lead to a missing AU -- one probably has a typo throw new TdbException( "Cannot add duplicate au entry: \"" + au.getName() + "\" with the same definition as \"" + existingAu.getName() + "\" for title \"" + titleName + "\" to title database"); } else { throw new TdbException( "Cannot add duplicate au entry: \"" + au.getName() + "\" for title \"" + titleName + "\" to title database"); } } // get or create the TdbTitle for this TdbTitle title = getTdbTitle(props, au); try { // add AU to title title.addTdbAu(au); } catch (TdbException ex) { // if we can't add au to title, remove for plugin and re-throw exception removeTdbAuForPlugin(au); throw ex; } // process title links Map<String, Map<String, String>> linkMap = new HashMap<String, Map<String, String>>(); for (Map.Entry<Object, Object> entry : props.entrySet()) { String key = "" + entry.getKey(); String value = "" + entry.getValue(); if (key.startsWith("journal.link.")) { // skip to link name String param = key.substring("link.".length()); int i; if (((i = param.indexOf(".type")) < 0) && ((i = param.indexOf(".journalId")) < 0)) { logger.warning( "Ignoring nexpected link key for au \"" + au.getName() + "\" key: \"" + key + "\""); } else { // get link map for linkName String lname = param.substring(0, i); Map<String, String> lmap = linkMap.get(lname); if (lmap == null) { lmap = new HashMap<String, String>(); linkMap.put(lname, lmap); } // add name and value to link map for link String name = param.substring(i + 1); lmap.put(name, value); } } } // add links to title from accumulated "type", "journalId" entries for (Map<String, String> lmap : linkMap.values()) { String name = lmap.get("type"); String value = lmap.get("journalId"); if ((name != null) && (value != null)) { try { TdbTitle.LinkType linkType = TdbTitle.LinkType.valueOf(name); title.addLinkToTdbTitleId(linkType, value); } catch (IllegalArgumentException ex) { logger.warning( "Ignoring unknown link type for au \"" + au.getName() + "\" name: \"" + name + "\""); } } } }
/** * Create a new TdbAu instance from the properties. * * @param props the properties * @return a TdbAu instance set built from the properties */ private TdbAu newTdbAu(Properties props) { String pluginId = (String) props.get("plugin"); if (pluginId == null) { throw new IllegalArgumentException("TdbAu plugin ID not specified"); } String auName = props.getProperty("title"); if (auName == null) { throw new IllegalArgumentException("TdbAu title not specified"); } // create a new TdbAu and set its elements TdbAu au = new TdbAu(auName, pluginId); // process attrs, and params Map<String, Map<String, String>> paramMap = new HashMap<String, Map<String, String>>(); for (Map.Entry<Object, Object> entry : props.entrySet()) { String key = String.valueOf(entry.getKey()); String value = String.valueOf(entry.getValue()); if (key.startsWith("attributes.")) { // set attributes directly String name = key.substring("attributes.".length()); try { au.setAttr(name, value); } catch (TdbException ex) { logger.warning( "Cannot set attribute \"" + name + "\" with value \"" + value + "\" -- ignoring"); } } else if (key.startsWith("param.")) { // skip to param name String param = key.substring("param.".length()); int i; if (((i = param.indexOf(".key")) < 0) && ((i = param.indexOf(".value")) < 0)) { logger.warning( "Ignoring unexpected param key for au \"" + auName + "\" key: \"" + key + "\" -- ignoring"); } else { // get param map for pname String pname = param.substring(0, i); Map<String, String> pmap = paramMap.get(pname); if (pmap == null) { pmap = new HashMap<String, String>(); paramMap.put(pname, pmap); } // add name and value to param map for pname String name = param.substring(i + 1); pmap.put(name, value); } } else if (!key.equals("title") // TdbAu has "name" property && !key.equals("plugin") // TdbAu has "pluginId" property && !key.equals("journalTitle") // TdbAu has "title" TdbTitle property && !key.startsWith("journal.")) { // TdbAu has "title" TdbTitle property // translate all other properties into AU properties try { au.setPropertyByName(key, value); } catch (TdbException ex) { logger.warning( "Cannot set property \"" + key + "\" with value \"" + value + "\" -- ignoring"); } } } // set param from accumulated "key", and "value" entries for (Map<String, String> pmap : paramMap.values()) { String name = pmap.get("key"); String value = pmap.get("value"); if (name == null) { logger.warning("Ignoring property with null name"); } else if (value == null) { logger.warning("Ignoring property \"" + name + "\" with null value"); } else { try { au.setParam(name, value); } catch (TdbException ex) { logger.warning( "Cannot set param \"" + name + "\" with value \"" + value + "\" -- ignoring"); } } } return au; }
/** * Merge other Tdb into this one. Makes copies of otherTdb's non-duplicate TdbPublisher, TdbTitle, * and TdbAu objects and their non-duplicate children. The object themselves are not merged. * * @param otherTdb the other Tdb * @throws TdbException if Tdb is sealed */ public void copyFrom(Tdb otherTdb) throws TdbException { // ignore inappropriate Tdb values if ((otherTdb == null) || (otherTdb == this)) { return; } if (isSealed()) { throw new TdbException("Cannot add otherTdb AUs to sealed Tdb"); } // merge non-duplicate publishers of otherTdb boolean tdbIsNew = tdbPublisherMap.isEmpty(); for (TdbPublisher otherPublisher : otherTdb.getAllTdbPublishers().values()) { String pubName = otherPublisher.getName(); TdbPublisher thisPublisher; boolean publisherIsNew = true; if (tdbIsNew) { // no need to check for existing publisher if TDB is new thisPublisher = new TdbPublisher(pubName); tdbPublisherMap.put(pubName, thisPublisher); } else { thisPublisher = tdbPublisherMap.get(pubName); publisherIsNew = (thisPublisher == null); if (publisherIsNew) { // copy publisher if not present in this Tdb thisPublisher = new TdbPublisher(pubName); tdbPublisherMap.put(pubName, thisPublisher); } } // merge non-duplicate titles of otherPublisher into thisPublisher for (TdbTitle otherTitle : otherPublisher.getTdbTitles()) { String otherId = otherTitle.getId(); TdbTitle thisTitle; boolean titleIsNew = true; if (publisherIsNew) { // no need to check for existing title if publisher is new thisTitle = otherTitle.copyForTdbPublisher(thisPublisher); thisPublisher.addTdbTitle(thisTitle); } else { thisTitle = thisPublisher.getTdbTitleById(otherId); titleIsNew = (thisTitle == null); if (titleIsNew) { // copy title if not present in this publisher thisTitle = otherTitle.copyForTdbPublisher(thisPublisher); thisPublisher.addTdbTitle(thisTitle); } else if (!thisTitle.getName().equals(otherTitle.getName())) { // error because it could lead to a missing title -- one probably has a typo // (what about checking other title elements too?) logger.error( "Ignorning duplicate title entry: \"" + otherTitle.getName() + "\" with the same ID as \"" + thisTitle.getName() + "\""); } } // merge non-duplicate TdbAus of otherTitle into thisTitle for (TdbAu otherAu : otherTitle.getTdbAus()) { // no need to check for existing au if title is new String pluginId = otherAu.getPluginId(); if (titleIsNew || !getTdbAuIds(pluginId).contains(otherAu.getId())) { // always succeeds we've already checked for duplicate TdbAu thisAu = otherAu.copyForTdbTitle(thisTitle); addTdbAuForPlugin(thisAu); } else { TdbAu thisAu = findExistingTdbAu(otherAu); if (!thisAu.getTdbTitle().getName().equals(otherAu.getTdbTitle().getName())) { if (!thisAu.getName().equals(otherAu.getName())) { logger.error( "Ignorning duplicate au entry: \"" + otherAu.getName() + "\" for title \"" + otherAu.getTdbTitle().getName() + "\" with same definion as existing au entry: \"" + thisAu.getName() + "\" for title \"" + thisAu.getTdbTitle().getName() + "\""); } else { logger.error( "Ignorning duplicate au entry: \"" + otherAu.getName() + "\" for title \"" + otherAu.getTdbTitle().getName() + "\" with same definion as existing one for title \"" + thisAu.getTdbTitle().getName() + "\""); } } else if (!thisAu.getName().equals(otherAu.getName())) { // error because it could lead to a missing AU -- one probably has a typo logger.error( "Ignorning duplicate au entry: \"" + otherAu.getName() + "\" with the same definition as \"" + thisAu.getName() + "\" for title \"" + otherAu.getTdbTitle().getName()); } else { logger.warning( "Ignoring duplicate au entry: \"" + otherAu.getName() + "\" for title \"" + otherAu.getTdbTitle().getName()); } } } } } }