<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Rule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Rule_Axmedis.xsd">
  <Header>
    <Rule_Name>MassiveScalingHorror</Rule_Name>
    <AXRID>axcprule:230472ad-3c4d-43a9-987c-2dfc06887f37</AXRID>
    <Rule_Version>1.0</Rule_Version>
    <Rule_Type>AXCP</Rule_Type>
    <Software_Name>AXCP Rule Editor</Software_Name>
    <Version_of_software>1.0</Version_of_software>
    <Date_of_production>2006-01-17</Date_of_production>
    <Author>Ivan Bruno</Author>
    <Affiliation>Disit</Affiliation>
    <URL>www.dsi.unifi.it/~ivanb</URL>
    <Comment></Comment>
    <Last_Modifications>2006-03-16</Last_Modifications>
    <Terminal_ID></Terminal_ID>
    <Cost></Cost>
    <Work_Item_ID></Work_Item_ID>
  </Header>
  <Schedule>
    <Run>
      <Date>2006-03-15</Date>
      <Time>17:45:49</Time>
      <Periodicity Unit="Day">0</Periodicity>
      <Expiration_Date>2007-01-17</Expiration_Date>
      <Expiration_Time>17:33:49</Expiration_Time>
    </Run>
    <Status>Inactive</Status>
  </Schedule>
  <Definition>
    <AXCP_Rule>
      <Arguments>
        <Parameter Name="res" Type="String">axmedis_logo.png</Parameter>
        <Parameter Name="resourcePath" Type="String">C:\axexamples\ruleditor\resources\</Parameter>
        <Parameter Name="h" Type="Integer">320</Parameter>
        <Parameter Name="w" Type="Integer">200</Parameter>
        <selection name="selection" timestamp="2006-03-13T15:59:00-00:00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Selection-v1-6.xsd">
          <query name="Query(0)" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="QUERY-v1-6.xsd">
            <source>
              <location>AXDB</location>
    </source>
            <result>
              <AXInfofield>DCMI:title</AXInfofield>
              <AXInfofield>DCMI:type</AXInfofield>
    </result>
            <AXinfoQuery>
              <querycondition>
                <nesting>
                  
                  <test>
                    <field>DCMI:type</field>
                    <operator>EQ</operator>
                    <value>horror</value>
          </test>
        </nesting>
      </querycondition>
    </AXinfoQuery>
  </query>
</selection>        <Parameter Name="format" Type="String">png</Parameter>
      
      </Arguments>
      <Rule_Body>
        <JS_Script><![CDATA[/***************************************************************************/
// AXMEDIS JSScript
// This script is provided merely as demonstration tools and provide 
// limited functionalities and robustness with respect to the full tools, 
// and as such should be considered. 
// AXMEDIS declines any responsibility on the use made with the script 
// hereby installed.
// The intellectual property of this script belongs to AXMEDIS, and 
// any total or partial reproduction of it is prohibited 
// without written authorization of AXMEDIS Consortium. 
// AXMEDIS Copyright 2006.

// Please verify the resourcePath value before running the script
// Fix it to the right path (double clicking on the item to open
// the editing dialog)
/**************************************************************************/


// Function for creating the Dublin Core information
function createDC(obj,title)
{
    dc = obj.getDublinCore();
    dc.addDCElement("creator","AXCP Rule Editor");
    dc.addDCElement("title",title);
    dc.addDCElement("type","conversion to "+format);
    dc.addDCElement("description","Testing JSScript with resized and converted images");
}

function adaptAxImages(axobj,height,width,mimeType)
{
  print("Creating adapted image");   
  var h1 =  new Array(1);
  var w1 =  new Array(1);
  h1[0]=0;
  w1[0]=0;
  var resource = axobj.getContent();
  var i = 0;
  for (i in resource)
  {
    var res = resource[i];
    if(res instanceof AxResource)
    {
        val = resource[i].mimeType.search("image");
        if(val==0)
        {
            ImageProcessing.GetInfo(resource[i],h1,w1);
            print("Original resource size is "+h1[0]+"x"+w1[0]);
            print("Resizing the resource at "+height+"x"+width);
            ImageProcessing.Resize(resource[i],height,width,false,resource[i]);
            
            ImageProcessing.GetInfo(resource[i],h1,w1);
            print("New resource size is "+h1[0]+"x"+w1[0]);
            if(resource[i].mimeType != format)
            {
                print("Converting the resource in "+format);
                ImageProcessing.Conversion(resource[i],mimeType,resource[i]);
            } 
        }
    }
    else if(res instanceof AxmedisObject)
        adaptAxImages(res,height,width,mimeType)
    
  }
}

function cloneAxmedisObject(sourceObj,targetObj)
{
        var metadata = sourceObj.getGenericMetadata();
        if(metadata!=null)
        {
            for(j in metadata)
                targetObj.addMetadata(metadata[j]);
        }
        targetObj.removeMetadata(targetObj.getAxInfo());
        targetObj.addMetadata(sourceObj.getAxInfo());
        var resources = sourceObj.getContent();
        if(resources!=null)
        {
            for(k in resources)
               targetObj.addContent(resources[k]);
        }
}

// Function for creating the Axmedis Object by composing and converting resources
function test()
{
  var imgFormat = getMimeType(format);
  selection.resolveQuery("test","test",0);
  var documentList = selection.getAXDBResult();
    
  for (i in documentList)
  {
        
         uri ="axdb://"+documentList[i]; 
         var axmedisObject = new AxmedisObject(uri);
         var dublinCore = axmedisObject.getDublinCore();
         var title = dublinCore.getDCElementValue("title")+"_Resized";
         var newObj = new AxmedisObject();
         cloneAxmedisObject(axmedisObject, newObj);
         createDC(newObj,title); 
         adaptAxImages(newObj,h,w,imgFormat);
         print("Storing Object on AXMEDIS Database");
         //newObj.save(resourcePath+title+".axm");
         newObj.uploadToDB();
  }
  return true;
  
}

//Entry point of the script
test();
]]>
        </JS_Script>
      </Rule_Body>
      <Dependencies>
        <Dependency>
          <Plug_In_name>ImageProcessing</Plug_In_name>
          <Version>1.001</Version>
        </Dependency>
      </Dependencies>
    </AXCP_Rule>
  </Definition>
</Rule>

