Scarab commit: svn commit: r10162 - branches/release/b21/src/java/org/tigris/scarab: om util/xmlissues

[email protected]
Newsgroups gmane.comp.java.scarab.cvs
Message-ID <[email protected]>
Author: hair
Date: 2006-06-11 04:20:24-0700
New Revision: 10162

Modified:
   branches/release/b21/src/java/org/tigris/scarab/om/Issue.java
   branches/release/b21/src/java/org/tigris/scarab/util/xmlissues/ScarabIssues.java

Log:
SCB1668:  (Add improved Bugzilla import XSLT scripts) 
 -   	SCB1668-ids.patch applied from Steve James.


Modified: branches/release/b21/src/java/org/tigris/scarab/om/Issue.java
Url: http://scarab.tigris.org/source/browse/scarab/branches/release/b21/src/java/org/tigris/scarab/om/Issue.java?view=diff&rev=10162&p1=branches/release/b21/src/java/org/tigris/scarab/om/Issue.java&p2=branches/release/b21/src/java/org/tigris/scarab/om/Issue.java&r1=10161&r2=10162
==============================================================================
--- branches/release/b21/src/java/org/tigris/scarab/om/Issue.java	(original)
+++ branches/release/b21/src/java/org/tigris/scarab/om/Issue.java	2006-06-11 04:20:24-0700
@@ -2049,6 +2049,15 @@
      * Removes any unset attributes and sets the issue # prior to saving
      * for the first time.  Calls super.save()
      *
+     * If the issue does not have an <i>idCount</i> then the next
+     * available ID is allocated.
+     *
+     * If the issue has a non-zero <i>idCount</i>, this value is honoured.
+     * WARNING: do not set the idCount to an existing ID!
+     * The nominated value is ignored if it is not at least as high as the 
+     * next available ID.
+     *
+     *
      * @param dbCon a <code>DBConnection</code> value
      * @exception TorqueException if an error occurs
      */
@@ -2090,6 +2099,16 @@
             {
                 try
                 {
+                    final int suggestedID = getIdCount();
+                    if (suggestedID != 0) {
+                        // Force the next available issue ID to be the
+                        // nominated value, if not out of sequence.
+                        // TODO: assert that this issue doesn't already exist
+                        // In this case, just skip the next action.
+                        setNextIssueId(dbCon, suggestedID);
+                    }
+                    
+                    // Set the ID to the next available value.
                     setIdCount(getNextIssueId(dbCon));
                 }
                 catch (Exception e)
@@ -2100,7 +2119,10 @@
         }
         super.save(dbCon);
     }
-    
+
+    /* Gets the next available issue ID.
+     * If the ID table doesn't yet exist, it is created.
+     */
     private int getNextIssueId(Connection con)
         throws TorqueException, ScarabException
     {
@@ -2126,7 +2148,7 @@
                     // entered, insert a row into the id_table and try again.
                     try
                     {
-                        saveIdTableKey(con);
+                        saveIdTableKey(con, 2);
                         id = 1;
                     }
                     catch (Exception badException)
@@ -2148,6 +2170,56 @@
         return id;
     }
 
+    /*
+     * Sets the next available issue ID to the given value
+     * If the ID table doesn't yet exist, it is created.
+     */
+    private void setNextIssueId(Connection con, int newID)
+        throws TorqueException, ScarabException
+    {
+        String key = getIdTableKey();
+        DatabaseMap dbMap = IssuePeer.getTableMap().getDatabaseMap();
+        IDBroker idbroker = dbMap.getIDBroker();
+        int nextID = 1;
+        
+        synchronized (idbroker)
+        {
+            try
+            {
+                // Check if the ID table is available and get the next ID
+                nextID = idbroker.getIdAsInt(con, key);
+            }
+            catch (Exception idRetrievalErr) {
+                // No, create the ID table now
+                saveIdTableKey(con, nextID);
+            }
+
+            if (nextID > newID) {
+                getLog()
+                    .error("New issue ID "+ newID
+                    + "is out of sequence. Must be at least " + nextID);
+            }
+            else
+            {
+                try
+                {
+                    // Now set the next available ID in the table
+                    setIdTableKey(con, newID);
+                }
+                catch (Exception badException)
+                {
+                    getLog()
+                        .error("Error creating ID_TABLE entry for "
+                               + getIdTableKey(), badException);
+                    // throw the original
+                    throw new ScarabException(
+                        L10NKeySet.ExceptionRetrievingIssueId,
+                        badException);
+                }
+            }
+        }
+    }
+
     private String getIdTableKey()
         throws TorqueException
     {
@@ -2162,7 +2234,7 @@
         return prefix;
     }
 
-    private void saveIdTableKey(final Connection dbCon)
+    private void saveIdTableKey(final Connection dbCon, final int nextID)
         throws TorqueException
     {
         int id = 0;
@@ -2185,10 +2257,24 @@
         // FIXME: UGLY! IDBroker doesn't have a Peer yet.
         final String sql = "insert into " + idTable 
          + " (ID_TABLE_ID,TABLE_NAME,NEXT_ID,QUANTITY) "
-         + " VALUES (" + id + ",'" + key + "',2,1)" ;
+         + " VALUES (" + id + ",'" + key + "'," + nextID + ",1)" ;
         BasePeer.executeStatement(sql, dbCon);
     }
 
+    /*
+     * Sets the next available ID to the given ID
+     */
+    private void setIdTableKey(final Connection dbCon, int id)
+        throws TorqueException
+    {
+        final String key = getIdTableKey();
+
+        // FIXME: UGLY! IDBroker doesn't have a Peer yet.
+        final String sql = "update ID_TABLE set NEXT_ID=" + id
+            + " where TABLE_NAME='" + key + "'";
+        BasePeer.executeStatement(sql, dbCon);
+    }
+    
     /**
      * Returns list of issue template types.
     public List getTemplateTypes() throws TorqueException

Modified: branches/release/b21/src/java/org/tigris/scarab/util/xmlissues/ScarabIssues.java
Url: http://scarab.tigris.org/source/browse/scarab/branches/release/b21/src/java/org/tigris/scarab/util/xmlissues/ScarabIssues.java?view=diff&rev=10162&p1=branches/release/b21/src/java/org/tigris/scarab/util/xmlissues/ScarabIssues.java&p2=branches/release/b21/src/java/org/tigris/scarab/util/xmlissues/ScarabIssues.java&r1=10161&r2=10162
==============================================================================
--- branches/release/b21/src/java/org/tigris/scarab/util/xmlissues/ScarabIssues.java	(original)
+++ branches/release/b21/src/java/org/tigris/scarab/util/xmlissues/ScarabIssues.java	2006-06-11 04:20:24-0700
@@ -862,7 +862,7 @@
         }
     }
 
-    private Issue createNewIssue(final XmlModule module, final XmlIssue issue)
+    private Issue createNewIssue(final XmlModule module, final XmlIssue issue, final String id)
         throws TorqueException,ScarabException
     {
         // get the instance of the module
@@ -872,6 +872,13 @@
         issueTypeOM.setName(issue.getArtifactType());
         // get me a new issue since we couldn't find one before
         final Issue issueOM = Issue.getNewInstance(moduleOM, issueTypeOM);
+
+        // The import data may nominate its ID
+        if (id != null) {
+            // This will cause Issue.save() to use this ID
+            issueOM.setIdCount(Integer.parseInt(id));
+        }
+        
         // create the issue in the database
         issueOM.save();
 
@@ -900,19 +907,29 @@
 /////////////////////////////////////////////////////////////////////////////////  
         // Get me an issue
         Issue issueOM = null;
+        final String issueID = issue.hasModuleCode() ? "" : module.getCode() + issue.getId();
         if (getImportTypeCode() == CREATE_SAME_DB || getImportTypeCode() == CREATE_DIFFERENT_DB)
         {
-            issueOM = createNewIssue(module, issue);
+            // Check if the new issue nominates an ID and if the database does
+            // not already contain an issue with that ID
+            if (issue.getId() != null && IssueManager.getIssueById(issueID) == null)
+            {
+                // Create the new issue with the nominated ID
+                issueOM = createNewIssue(module, issue, issue.getId());
+            }
+            else
+            {
+                // Crate the new issue with an automatically allocated ID
+                issueOM = createNewIssue(module, issue, null);
+            }
         }
         else if (getImportTypeCode() == UPDATE_SAME_DB) // nice to specify just for searching/refactoring
         {
-            issueOM = IssueManager.getIssueById(
-                    (issue.hasModuleCode()?"":module.getCode())
-                    + issue.getId());
+            issueOM = IssueManager.getIssueById(issueID);
             
             if (issueOM == null)
             {
-                issueOM = createNewIssue(module, issue);
+                issueOM = createNewIssue(module, issue, null);
             }
             else
             {
@@ -1360,7 +1377,7 @@
 
     private boolean isDuplicateDependency(final XmlActivitySet activitySet)
     {
-        return (dependActivitySetId.indexOf(activitySet.getId())<0);
+        return (dependActivitySetId.indexOf(activitySet.getId()) > -1);
     }
 
     private Activity createActivity(final XmlActivity activity,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.