<?xml version="1.0" encoding="utf-16"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="de-DE">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Action">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="OID" type="xs:int" minOccurs="0" />
                <xs:element name="ObjectKey" type="xs:string" minOccurs="0" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
                <xs:element name="TargetCID" type="xs:int" minOccurs="0" />
                <xs:element name="ClassType" type="xs:int" minOccurs="0" />
                <xs:element name="ActionType" type="xs:string" minOccurs="0" />
                <xs:element name="Description" type="xs:string" minOccurs="0" />
                <xs:element name="IsActive" type="xs:short" minOccurs="0" />
                <xs:element name="IsSystemObject" type="xs:short" minOccurs="0" />
                <xs:element name="MenuUsage" type="xs:short" minOccurs="0" />
                <xs:element name="AssemblySearchPaths" type="xs:string" minOccurs="0" />
                <xs:element name="AssemblyReferences" type="xs:string" minOccurs="0" />
                <xs:element name="RequiredFeatures" type="xs:string" minOccurs="0" />
                <xs:element name="RequiresUIAccess" type="xs:short" minOccurs="0" />
                <xs:element name="InsUsr" type="xs:int" minOccurs="0" />
                <xs:element name="InsTS" type="xs:dateTime" minOccurs="0" />
                <xs:element name="UpdUsr" type="xs:int" minOccurs="0" />
                <xs:element name="UpdTS" type="xs:dateTime" minOccurs="0" />
                <xs:element name="ITSEVENT2ACTION" minOccurs="0" maxOccurs="unbounded">
                  <xs:annotation>
                    <xs:appinfo>
                      <msdata:Relationship name="Action_OID--_x003E_ITSEVENT2ACTION_ACTIONOID" msdata:parent="Action" msdata:child="ITSEVENT2ACTION" msdata:parentkey="OID" msdata:childkey="ACTIONOID" />
                    </xs:appinfo>
                  </xs:annotation>
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="EVENTOID" type="xs:int" minOccurs="0" />
                      <xs:element name="ACTIONOID" type="xs:int" minOccurs="0" />
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
                <xs:element name="ActionRef" minOccurs="0" maxOccurs="unbounded">
                  <xs:annotation>
                    <xs:appinfo>
                      <msdata:Relationship name="Action_OID--_x003E_ActionRef_INCLUDINGACTIONOID" msdata:parent="Action" msdata:child="ActionRef" msdata:parentkey="OID" msdata:childkey="INCLUDINGACTIONOID" />
                    </xs:appinfo>
                  </xs:annotation>
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="OID" type="xs:int" minOccurs="0" />
                      <xs:element name="InsUsr" type="xs:int" minOccurs="0" />
                      <xs:element name="InsTS" type="xs:dateTime" minOccurs="0" />
                      <xs:element name="UpdUsr" type="xs:int" minOccurs="0" />
                      <xs:element name="UpdTS" type="xs:dateTime" minOccurs="0" />
                      <xs:element name="INCLUDINGACTIONOID" type="xs:int" minOccurs="0" />
                      <xs:element name="REFERENCEDACTIONOID" type="xs:int" minOccurs="0" />
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
                <xs:element name="ObjectData" minOccurs="0" maxOccurs="unbounded">
                  <xs:annotation>
                    <xs:appinfo>
                      <msdata:Relationship name="Action_oid--_x003E_ObjectData_objectoid" msdata:parent="Action" msdata:child="ObjectData" msdata:parentkey="OID" msdata:childkey="ObjectOID" />
                    </xs:appinfo>
                  </xs:annotation>
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="OID" type="xs:int" minOccurs="0" />
                      <xs:element name="ObjectCID" type="xs:int" minOccurs="0" />
                      <xs:element name="ObjectOID" type="xs:int" minOccurs="0" />
                      <xs:element name="DataType" type="xs:short" minOccurs="0" />
                      <xs:element name="Data" type="xs:string" minOccurs="0" />
                      <xs:element name="InsUsr" type="xs:int" minOccurs="0" />
                      <xs:element name="InsTS" type="xs:dateTime" minOccurs="0" />
                      <xs:element name="UpdUsr" type="xs:int" minOccurs="0" />
                      <xs:element name="UpdTS" type="xs:dateTime" minOccurs="0" />
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
                <xs:element name="LocalizedProperty" minOccurs="0" maxOccurs="unbounded">
                  <xs:annotation>
                    <xs:appinfo>
                      <msdata:Relationship name="Action_oid--_x003E_LocalizedProperty_objectoid" msdata:parent="Action" msdata:child="LocalizedProperty" msdata:parentkey="OID" msdata:childkey="ObjectOID" />
                    </xs:appinfo>
                  </xs:annotation>
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="OID" type="xs:int" minOccurs="0" />
                      <xs:element name="ObjectCID" type="xs:int" minOccurs="0" />
                      <xs:element name="ObjectOID" type="xs:int" minOccurs="0" />
                      <xs:element name="LanguageID" type="xs:int" minOccurs="0" />
                      <xs:element name="PropertyID" type="xs:int" minOccurs="0" />
                      <xs:element name="Value" type="xs:string" minOccurs="0" />
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Action>
    <ObjectKey>OXA_004</ObjectKey>
    <Name>Syncos Dialog - Pre process</Name>
    <TargetCID>-1</TargetCID>
    <ClassType>1</ClassType>
    <Description>Diese Aktion wird als Vorverarbeitung für den Auftragsimport ausgeführt. Für bereits in Syncos vorhandene Aufträge und Vorgänge wird der Auftragsstatus in die Dialog Datenbank übernommen.</Description>
    <IsActive>-1</IsActive>
    <IsSystemObject>0</IsSystemObject>
    <MenuUsage>0</MenuUsage>
    <RequiresUIAccess>0</RequiresUIAccess>
    <InsTS>2018-12-11T08:32:35+01:00</InsTS>
    <UpdTS>2021-01-08T09:09:22+01:00</UpdTS>
    <ObjectData>
      <ObjectCID>51</ObjectCID>
      <DataType>0</DataType>
      <Data>'{C#}//$ using System.Data;
//$ param object Logger;
//$ param object DialogConnection;
//$ param object SyncosSession;

var session = (Session)SyncosSession;
var dialogConnection = (DbConnection)DialogConnection;
var logger = (ITS.Support.Logger)Logger;

var os = session.GetObjectStore();
var syncosConnection = session.GetConnection();

try
{
    if (logger != null)
        logger.Log("Update job state", LogLevel.Debug);
    UpdateJobStateData("JobState", "JobImport", "State", "ITSJOB", false, dialogConnection, syncosConnection, logger);
    if (logger != null)
        logger.Log("Update OrderOS state", LogLevel.Debug);
    UpdateJobStateData("OOSState", "OrderOSImport", "OOSState", "ITSORDEROS", true, dialogConnection, syncosConnection, logger);

    if (logger != null)
        logger.Log("Update PlanKey", LogLevel.Debug);
    UpdatePlanKey(dialogConnection, session, logger);
}
catch (Exception ex)
{
    if (logger != null)
        logger.Log("Execption: " + ex.Message, LogLevel.Error);
}
}

private static void UpdateJobStateData(string dialogStateField, string dialogTableName, string syncosStateField, string syncosTableName, bool stateIsShortInt, DbConnection dialogConn, DbConnection syncosConnection, Logger logger)
{
    // Fetch rows from DEX
    var sqlStatement = string.Format("Select ID, ObjectKey, {0} from {1} where ImportState is null", dialogStateField, dialogTableName);
    IDbDataAdapter dba = dialogConn.CreateDataAdapter(sqlStatement);
    dba.InsertCommand = dialogConn.CreateInsertCommand(dba);
    dba.UpdateCommand = dialogConn.CreateUpdateCommand(dba);

    using (DataSet ds = new DataSet())
    {
        ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
        dba.Fill(ds);
        
        DataRowCollection rows = ds.Tables[0].Rows;
        // Update data (Syncos -&gt; DEX)
        foreach (DataRow row in rows)
        {
            string objectKey = (string)row["ObjectKey"];
            sqlStatement = string.Format("Select {0} from ITSDEV.{1} Where OBJECTKEY = '{2}'", syncosStateField, syncosTableName, objectKey);

            int state;
            if (GetIntValue(sqlStatement, syncosConnection, stateIsShortInt, logger, out state))
                row[dialogStateField] = state;
            else
                row[dialogStateField] = 1; // Freigegeben
        }
    
        // Commit changes
        dba.Update(ds);
    }
}

private static void UpdatePlanKey(DbConnection dialogConn, Session syncosSession, Logger logger)
{
    // Fetch rows from DEX
    var sqlStatement = string.Format("Select ID, PlanKey, PlanVersion, PartKey, PartVariant, PartVersion from JobImport where ImportState is null and PlanKey is null");
    IDbDataAdapter dba = dialogConn.CreateDataAdapter(sqlStatement);
    dba.InsertCommand = dialogConn.CreateInsertCommand(dba);
    dba.UpdateCommand = dialogConn.CreateUpdateCommand(dba);

    using (DataSet ds = new DataSet())
    {
        ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
        dba.Fill(ds);
        
        DataRowCollection rows = ds.Tables[0].Rows;
        foreach (DataRow row in rows)
        {
            string partKey = (string)row["PartKey"];
            string partVariant = (string)row["PartVariant"];
            int? partVersion = row.IsNull("PartVersion") ? new int?() : new int?(Utilities.ConvertTo&lt;int&gt;(row["PartVersion"]));
            int? planVersion = row.IsNull("PlanVersion") ? new int?() : new int?(Utilities.ConvertTo&lt;int&gt;(row["PlanVersion"]));

            var parts = syncosSession.GetObjectStore().FetchInstances&lt;ITS.BusinessLayer.Part&gt;(new AndQualifier(
                new KeyValueQualifier("ObjectKey", EComparisonOperator.Equal, partKey),
                new KeyValueQualifier("VariantNumber", EComparisonOperator.Equal, partVariant)))
                .ToList();

            if (parts.Count == 0)
                break;

            if (partVersion.HasValue)
                parts = parts.Where(x =&gt; x.SystemVersion == partVersion.Value).ToList();
            else
                parts = parts.OrderByDescending(x =&gt; x.SystemVersion).ToList();
            var part = parts.FirstOrDefault();
            
            var plans = part.Plans.Where(x =&gt; x.VersionState == EVersionState.Released).ToList();            
            if (plans.Count == 0)
                plans = part.PartFamily?.Plans.Where(x =&gt; x.VersionState == EVersionState.Released).ToList();

            if (plans.Count == 0)
                break;
            if (planVersion.HasValue)
                plans = plans.Where(x =&gt; x.SystemVersion == planVersion.Value).ToList();
            else 
                plans = plans.OrderByDescending(x =&gt; x.SystemVersion).ToList();
            var plan = plans.FirstOrDefault();

            row["PlanKey"] = plan?.ObjectKey;
        }
    
        // Commit changes
        dba.Update(ds);
    }
}

private static bool GetIntValue(string sqlStatement, DbConnection connection, bool stateIsShortInt, Logger logger, out int state)
{
    state = 0;
    using (var command = connection.CreateCommand(connection.NativeConnection, System.Data.CommandType.Text, sqlStatement))
    {
        using (var reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                if (stateIsShortInt)
                    state = reader.GetInt16(0);
                else
                    state = reader.GetInt32(0);
                return true;
            }
        }
    }
    
    return false;
}

private static void EndOfScript()
{</Data>
      <InsTS>2018-12-11T08:32:35+01:00</InsTS>
      <UpdTS>2021-01-08T09:09:22+01:00</UpdTS>
      <PrimaryKey Entity="ObjectData">
        <OID>1020</OID>
      </PrimaryKey>
      <ObjRef Entity="Action" OID="130">
        <FKProp>ObjectOID</FKProp>
      </ObjRef>
      <ObjRef Entity="User" OID="1">
        <FKProp>InsUsr</FKProp>
      </ObjRef>
      <ObjRef Entity="User" OID="1">
        <FKProp>UpdUsr</FKProp>
      </ObjRef>
    </ObjectData>
    <LocalizedProperty>
      <ObjectCID>51</ObjectCID>
      <LanguageID>9</LanguageID>
      <PropertyID>167945272</PropertyID>
      <Value>Syncos Dialog - Pre process</Value>
      <PrimaryKey Entity="LocalizedProperty">
        <OID>495040</OID>
      </PrimaryKey>
      <ObjRef Entity="Action" OID="130">
        <FKProp>ObjectOID</FKProp>
      </ObjRef>
    </LocalizedProperty>
    <LocalizedProperty>
      <ObjectCID>51</ObjectCID>
      <LanguageID>31</LanguageID>
      <PropertyID>167945272</PropertyID>
      <Value>Syncos Dialog - Pre process</Value>
      <PrimaryKey Entity="LocalizedProperty">
        <OID>495041</OID>
      </PrimaryKey>
      <ObjRef Entity="Action" OID="130">
        <FKProp>ObjectOID</FKProp>
      </ObjRef>
    </LocalizedProperty>
    <PrimaryKey Entity="Action">
      <OID>130</OID>
    </PrimaryKey>
    <ObjRef Entity="User" OID="1" External="">
      <FKProp>InsUsr</FKProp>
      <KeyVals>
        <ClassID>47</ClassID>
        <Name>Administrator</Name>
      </KeyVals>
    </ObjRef>
    <ObjRef Entity="User" OID="1">
      <FKProp>UpdUsr</FKProp>
    </ObjRef>
  </Action>
  <InfoHeader Entity="Action" ReqDbSchemaLevel="169" DbSchemaLevel="194" SystemCurrency="EUR" />
</NewDataSet>