Browse Source

#906 Better way to handle the lock in order to prevent duplicates in the numbering of Tickets. Note that the iTopMutex now supports re-entrancy inside the same PHP page.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3230 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 11 years ago
parent
commit
f080016d81

+ 0 - 14
datamodels/2.x/itop-change-mgmt-itil/datamodel.itop-change-mgmt-itil.xml

@@ -932,20 +932,6 @@
 		$this->Set('last_update', time());
 	}]]></code>
         </method>
-        <method id="ComputeValues">
-          <static>false</static>
-          <access>public</access>
-          <type>Overload-DBObject</type>
-          <code><![CDATA[	public function ComputeValues()
-	{
-		if ($this->IsNew())
-		{
-			$iKey = MetaModel::GetNextKey(get_class($this));
-			$sName = sprintf('C-%06d', $iKey);
-			$this->Set('ref', $sName);
-		}
-	}]]></code>
-        </method>
         <method id="GetIcon">
           <comment>/**&#13;
 	 * Get the icon representing this object&#13;

+ 0 - 14
datamodels/2.x/itop-change-mgmt/datamodel.itop-change-mgmt.xml

@@ -633,20 +633,6 @@
 		$this->Set('last_update', time());
 	}]]></code>
         </method>
-        <method id="ComputeValues">
-          <static>false</static>
-          <access>public</access>
-          <type>Overload-DBObject</type>
-          <code><![CDATA[	public function ComputeValues()
-	{
-		if ($this->IsNew())
-		{
-			$iKey = MetaModel::GetNextKey(get_class($this));
-			$sName = sprintf('C-%06d', $iKey);
-			$this->Set('ref', $sName);
-		}
-	}]]></code>
-        </method>
         <method id="GetIcon">
           <comment>/**&#13;
 	 * Get the icon representing this object&#13;

+ 0 - 7
datamodels/2.x/itop-incident-mgmt-itil/datamodel.itop-incident-mgmt-itil.xml

@@ -1277,13 +1277,6 @@
 		// Compute the priority of the ticket
 		$this->Set('priority', $this->ComputePriority());
 
-		if ($this->IsNew())
-		{
-			$iKey = MetaModel::GetNextKey(get_class($this));
-			$sName = sprintf('I-%06d', $iKey);
-			$this->Set('ref', $sName);
-		}
-
 		return parent::ComputeValues();
 	}]]></code>
         </method>

+ 0 - 7
datamodels/2.x/itop-problem-mgmt/datamodel.itop-problem-mgmt.xml

@@ -504,13 +504,6 @@
 	{
 		// Compute the priority of the ticket
 		$this->Set('priority', $this->ComputePriority());
-
-		if ($this->IsNew())
-		{
-			$iKey = MetaModel::GetNextKey(get_class($this));
-			$sName = sprintf('P-%06d', $iKey);
-			$this->Set('ref', $sName);
-		}
 	}]]></code>
         </method>
         <method id="OnInsert">

+ 0 - 8
datamodels/2.x/itop-request-mgmt-itil/datamodel.itop-request-mgmt-itil.xml

@@ -1667,14 +1667,6 @@
 		// Compute the priority of the ticket
 		$this->Set('priority', $this->ComputePriority());
 
-		if ($this->IsNew())
-		{
-			// Object not yet in the Database
-			$iKey = MetaModel::GetNextKey(get_class($this));
-			$sName = sprintf('R-%06d', $iKey);
-			$this->Set('ref', $sName);
-		}
-
 		return parent::ComputeValues();
 	}]]></code>
         </method>

+ 0 - 7
datamodels/2.x/itop-request-mgmt/datamodel.itop-request-mgmt.xml

@@ -1653,13 +1653,6 @@
 		// Compute the priority of the ticket
 		$this->Set('priority', $this->ComputePriority());
 
-		if ($this->IsNew())
-		{
-			$iKey = MetaModel::GetNextKey(get_class($this));
-			$sName = sprintf('R-%06d', $iKey);
-			$this->Set('ref', $sName);
-		}
-
 		return parent::ComputeValues();
 	}]]></code>
         </method>

+ 41 - 25
datamodels/2.x/itop-tickets/datamodel.itop-tickets.xml

@@ -48,7 +48,7 @@
         <field id="ref" xsi:type="AttributeString">
           <sql>ref</sql>
           <default_value/>
-          <is_null_allowed>false</is_null_allowed>
+          <is_null_allowed>true</is_null_allowed>
         </field>
         <field id="org_id" xsi:type="AttributeExternalKey">
           <sql>org_id</sql>
@@ -164,37 +164,53 @@
           <static>false</static>
           <access>public</access>
           <type>Overload-DBObject</type>
-          <code><![CDATA[    public function DBInsertNoReload()
+          <code><![CDATA[
+    public function DBInsertNoReload()
     {
-      $oMutex = new iTopMutex('ticket_insert');
-      $oMutex->Lock();
-      $iKey = parent::DBInsertNoReload();
-      $oMutex->Unlock();
-      return $iKey;
+          $oMutex = new iTopMutex('ticket_insert');
+          $oMutex->Lock();
+          $iNextId = MetaModel::GetNextKey(get_class($this));
+          $sRef = $this->MakeTicketRef($iNextId);
+          $this->Set('ref', $sRef);
+          $iKey = parent::DBInsertNoReload();
+          $oMutex->Unlock();
+          return $iKey;
     }
         ]]></code>
         </method>
-        <method id="DBInsertTracked_Internal">
+        <method id="MakeTicketRef">
           <static>false</static>
           <access>protected</access>
           <type>Overload-DBObject</type>
-          <code><![CDATA[    protected function DBInsertTracked_Internal($bDoNotReload = false)
-    {
-      // Beware !!!
-      // Compensate the fact that CMDBObject::DBInsertTracked_Internal does NOT call the derived version of DBInsertNoReload
-      // when performing an INsert with "no reload" but actually calls it (followed by Reload) when doing an Insert with reload !!
-      if ($bDoNotReload)
-      {
-          $oMutex = new iTopMutex('ticket_insert');
-          $oMutex->Lock();
-      }
-      $ret = parent::DBInsertTracked_Internal($bDoNotReload);
-      if ($bDoNotReload)
-      {
-          $oMutex->Unlock();
-      }
-      return $ret;
-    }
+          <code><![CDATA[
+        protected function MakeTicketRef($iNextId)
+        {
+                switch(get_class($this))
+                {
+                        case 'UserRequest':
+                        $sFormat = 'R-%06d';
+                        break;
+
+                        case 'Incident':
+                        $sFormat = 'I-%06d';
+                        break;
+
+                        case 'Change':
+                        case 'RoutineChange':
+                        case 'EmergencyChange':
+                        case 'NormalChange':
+                        $sFormat = 'C-%06d';
+                        break;
+
+                        case 'Problem':
+                        $sFormat = 'P-%06d';
+                        break;
+
+                        default:
+                        $sFormat = 'T-%06d';
+                }
+                return sprintf($sFormat, $iNextId);
+        }
         ]]></code>
         </method>
        </methods>