<?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>searchbox-tiscali-4</Rule_Name>
    <AXRID></AXRID>
    <Rule_Version>4</Rule_Version>
    <Rule_Type>AXCP</Rule_Type>
    <Software_Name></Software_Name>
    <Version_of_software></Version_of_software>
    <Date_of_production>2005-11-21</Date_of_production>
    <Author>Pierfrancesco Bellini</Author>
    <Affiliation>DSI - Univ. of FLorence</Affiliation>
    <URL></URL>
    <Comment>Rule to create axmedis objects from the mediaclub CMS</Comment>
    <Last_Modifications>2007-03-22</Last_Modifications>
    <Terminal_ID></Terminal_ID>
    <Cost></Cost>
    <Work_Item_ID></Work_Item_ID>
  </Header>
  <Schedule>
    <Run>
      <Date>2005-11-21</Date>
      <Time>17:41:51</Time>
      <Periodicity Unit="Day">0</Periodicity>
      <Expiration_Date>2005-11-21</Expiration_Date>
      <Expiration_Time>17:41:51</Expiration_Time>
    </Run>
    <Status>Inactive</Status>
  </Schedule>
  <Definition>
    <AXCP_Rule>
      <Arguments>
        <Parameter Name="importQuery" Type="String">genre:commedia</Parameter>
        <Parameter Name="uploadOnDB" Type="Boolean">false</Parameter>
        <Parameter Name="buildCompositeObject" Type="String">embed</Parameter>
        <Parameter Name="savePath" Type="String">C:/Documents and Settings/Piero/Documenti/axexamples/mediaclub-new</Parameter>
        <Parameter Name="forceNewObject" Type="Boolean">true</Parameter>
      
      </Arguments>
      <Rule_Body>
        <JS_Script name="MediaClub"><![CDATA[sb=new AXSearchbox
sb.host="localhost"
sb.port="2200"
sb.username="admin"
sb.password="password"

var r=new Array
cmsQuery("film",importQuery,r)
print("Starting to import " + r.length + " films...");
for(i=0; i<r.length; i++)
{
    var docid=r[i].id
    print("building movie "+(i+1))
    buildMovie(docid)
}

function buildMovie(docid)
{ 
    var axobj=buildFilmObject(docid);

    if(uploadOnDB)
    {
        axobj.uploadToDB()
        storeAXOID(docid,axobj.AXOID)
    }
    else
        axobj.save(savePath+"/films/"+axobj.contentID+".axm")
    axobj=null
}

function buildFilmObject(docid)
{
    newObject=true
    var m=sb.getDocumentMetadata(docid)
    var links=sb.getDocumentOutlinks(docid);
    
    //printCmsMetadata(m)
    
    title=m.getValue("Title")
    axoid=m.getValue("axoid")
    print("building film "+title+"...")
    
    if(forceNewObject)
        axobj=new AxmedisObject
    else if(!uploadOnDB || axoid=="")
    {
        if(!uploadOnDB && existsFile(savePath+"/films/"+toID(title)+".axm"))
        {
            print("  loading old object from file...")
            axobj=new AxmedisObject(savePath+"/films/"+toID(title)+".axm")
        }
        else
            axobj=new AxmedisObject
    }
    else
    {
        print("  retrieving old object from DB...")
        axobj=new AxmedisObject("axdb://"+axoid)
        newobject=false
    }
    
    axFilmDC=axobj.getDublinCore()
    updateAxInfo(axobj.getAxInfo())
    axobj.getAxInfo().objectRevision=-1
    
    updateDCMetadata(axFilmDC, "title", title, "ita")
        
    type=m.getValue("Type")
    updateDCMetadata(axFilmDC, "type", type, "ita")

    date=m.getValue("date")
    updateDCMetadata(axFilmDC, "date", date)

    description=m.getValue("Description")
    updateDCMetadata(axFilmDC, "description", description, "ita")
         
    posterPath=m.getValue("posterPath")
    if(posterPath!="")
    {
        axres=addResourceById(axobj,"poster.jpg")
        axres.load(posterPath)
        axlogo=new AxResource
        axlogo.load("C:/Documents and Settings/Piero/Documenti/Immagini/axlogo.png")
        ImageProcessing.Paste(axres,axlogo,0,0,1,axres)
    }

    plot=m.getValue("plot")
    if(plot!="")
    {
        writeToFile("c:/tmp.htm", 
        "<html><head><meta content='text/html;charset=utf-8' http-equiv='content-type'></head>" +
        "<body><h1>"+title+"</h1>"+
        "<a href='poster.jpg'><img src='poster.jpg' height='200'/></a><br><br>"+
        plot+"<br><br>"+
        "<img src='image.jpg'/></body></html>")
        axres=addResourceById(axobj,"plot.htm")
        axres.load("C:/tmp.htm")
        removeFile("C:/tmp.htm")
    }

    imagePath=m.getValue("imagePath")
    if(imagePath!="")
    {
        axres=addResourceById(axobj,"image.jpg")
        axres.load(imagePath)
    }

    downloadPreviewUrl=m.getValue("downloadPreviewUrl")
    if(downloadPreviewUrl!="")
    {
        axres=addResourceById(axobj,"preview-download")
        axres.ref=downloadPreviewUrl
        axres.mimeType="video/x-ms-wmv"
    }
    streamPreview512Url=m.getValue("streamPreview512Url")
    if(streamPreview512Url!="")
    {
        axres=addResourceById(axobj,"preview-stream-512")
        axres.ref=streamPreview512Url
        axres.mimeType="video/x-ms-wmv"
    }

    directorid=m.getValue("directorid")
    if(directorid!="")
    {
        rs=buildPersonObject(directorid, "directors")
        if(rs!=null)
        {
            updateDCMetadata(axFilmDC, "creator", rs[0])
            if(buildCompositeObject=="embed")
            {
                removeContentById(axobj,rs[1].contentID)
                axobj.addContent(rs[1])
            }
            if(uploadOnDB)
            {
                rs[1].uploadToDB()
                storeAXOID(rs[2], rs[1].AXOID)
            }
            else
                rs[1].save(savePath+"/directors/"+toID(rs[0])+".axm")
        }
        else
            ; //print("    director: "+directorid+ " not found")
    }
    
    actorids=m.getValues("actorid")
    for(actor in actorids)
    {
        rs=buildPersonObject(actorids[actor], "actors")
        if(rs!=null)
        {
            updateDCMetadata(axFilmDC, "contributor", rs[0])
            if(buildCompositeObject=="embed")
            {
                removeContentById(axobj,rs[1].contentID)
                axobj.addContent(rs[1])
            }
            if(uploadOnDB)
            {
                rs[1].uploadToDB()
                storeAXOID(rs[2], rs[1].AXOID)
            }
            else
                rs[1].save(savePath+"/actors/"+toID(rs[0])+".axm")
        }
        else
            ; //print("    actor: "+actorids[actor]+ " not found")
    }
    
    axobj.contentID=toID(title)
    return axobj
}

function buildPersonObject(personid, type)
{
    persons=new Array
    if(cmsQuery("person","personid:"+personid, persons)>0)
    {
        pm=sb.getDocumentMetadata(persons[0].id)
        
        name=pm.getValue("name")
        axoid=pm.getValue("axoid")
        contentId=toID(name)
        
        newobject=true
        print("    building person "+name+"...")
        if(forceNewObject)
            axPersonObj=new AxmedisObject
        else if(!uploadOnDB || axoid=="")
        {
           if(!uploadOnDB && existsFile(savePath+"/"+type+"/"+toID(name)+".axm"))
            {
                print("      loading old object from file...")
                axPersonObj=new AxmedisObject(savePath+"/"+type+"/"+toID(name)+".axm")
            }
            else
                axPersonObj=new AxmedisObject
        }
        else
        {
            print("      retrieving old object...")
            axPersonObj=new AxmedisObject("axdb://"+axoid)
            newobject=false
        }

        ret=new Array
        axPersonDC=axPersonObj.getDublinCore()
        updateAxInfo(axPersonObj.getAxInfo())
        
        if(name!="")
        {
            axPersonObj.contentID=contentId
            updateDCMetadata(axPersonDC,"title", name)
        }
        
        bio=pm.getValue("bio")
        if(bio!="")
        {
            writeToFile("c:/tmp.htm","<html><head><meta content='text/html;charset=utf-8' http-equiv='content-type'></head>"+
              "<body><h1>"+name+" - Biografia</h1>"+
              "<img src='picture.jpg'/><br><br>"+bio+"<br><br>"+
              "<a href='filmography.htm'>Filmografia</a></body></html>")
            axres=addResourceById(axPersonObj,contentId+"/biography.htm")
            axres.load("C:/tmp.htm")
            removeFile("C:/tmp.htm")
        }
        picturePath=pm.getValue("picturePath")
        if(picturePath!="")
        {
            axres=addResourceById(axPersonObj,contentId+"/picture.jpg")
            axres.load(picturePath)
        }
        filmography=pm.getValue("filmography")
        if(filmography!="")
        {
            writeToFile("c:/tmp.htm","<html><head><meta content='text/html;charset=utf-8' http-equiv='content-type'></head>"+
              "<body><h1>"+name+" - Filmografia</h1>"+filmography+"</body></html>")
            axres=addResourceById(axPersonObj,contentId+"/filmography.htm")
            axres.load("C:/tmp.htm")
            removeFile("C:/tmp.htm")
        }
        ret[0]=name
        ret[1]=axPersonObj
        ret[2]=persons[0].id
        return ret
    }
    return null
}

function updateAxInfo(axinfo)
{
    if(axinfo.objectCreatorCount==1 && axinfo.getObjectCreatorAXCID(0)=="")
    {
        axinfo.setObjectCreatorAXCID(0,"CRE_a45168bf-d02d-4ba6-b4ad-c4e94b270369")
        axinfo.setObjectCreatorName(0,"automatic importer")
        axinfo.setObjectCreatorNationality(0,"ITA")
    }
    axinfo.distributorName="TISCALI"
    axinfo.objectVersion=1
    axinfo.objectRevision=0
}

function putLogo(resource, logo)
{
    imageprocessingplugin.Paste(resource,logo,0,0,1,resource)
}]]>
        </JS_Script>
        <JS_Script name="Common AXMEDIS">
          
          <![CDATA[function updateDCMetadata(axDC, dcName, dcValue, dcLang)
{
    if(axDC.getDCElementCount(dcName)==0)
    {
        if(dcValue!="")
        {
            if(dcLang==null)
                axDC.addDCElement(dcName, dcValue)
            else
                axDC.addDCElement(dcName, dcValue, dcLang)
        }
    }
    else
    {
        axDC.setDCElementValue(dcName,0,dcValue)
        if(dcLang!=null)
            axDC.setDCElementLanguage(dcName,0,dcLang)
    }
}
function addResourceById(axobj, id)
{
    content=axobj.getContent()
    if(content!=null)
    {
        for(xx=0; xx<content.length; xx++)
            if(content[xx].contentID==id)
                return content[xx]
    }
    axres1=new AxResource;
    axres2=axobj.addContent(axres1);
    axres2.contentID=id
    return axres2
}
function removeContentById(axobj, id)
{
    content=axobj.getContent()
    if(content!=null)
    {
        for(xx=0; xx<content.length; xx++)
            if(content[xx].contentID==id)
            {
                axobj.removeContent(content[xx])
                return
            }
    }
    return
}
function toID(str)
{
    while(str.indexOf(" ")>=0)
        str=str.replace(" ","-")
    return str
}
function xmlEncode(str)
{
    while(str.indexOf("<")>=0)
        str=str.replace("<","&lt;")
    while(str.indexOf(">")>=0)
        str=str.replace(">","&gt;")
    while(str.indexOf("<")>=0)
        str=str.replace("<","&lt;")
    return str
}]]>
        </JS_Script>
        <JS_Script name="Common SearchBox-mediaclub">
          
          <![CDATA[
function cmsQuery(archive, queryString, results)
{
    var qs=new QuerySpec;
    if(archive=="film")
        a = [17];
    else if(archive=="person")
        a = [15];
    else
        return 0
    qs.archives = a;
    qs.info=QueryInfo.INFO_ALL_METADATA
    //qs.firstDoc = 0;
    qs.lastDoc = 1000;
    qs.queryString=queryString
    return sb.query(qs,results);
}
function storeAXOID(sbDocId, axoid)
{
    templateMetadata= [ new MetadataValue ]
    templateMetadata[0].key="axoid"
    templateMetadata[0].slice=13
    templateMetadata[0].value=axoid
    sb.applyMetadataTemplate(sbDocId,3,templateMetadata)
}
function printCmsMetadata(m)
{
    for(x=0; x<m.length; x++)
        print(m[x].key+" --> "+m[x].value)
}]]>
        </JS_Script>
      </Rule_Body>
      <Dependencies>
        <Dependency>
          <Plug_In_name>ImageProcessing</Plug_In_name>
          <Version>1.001</Version>
        </Dependency>
      </Dependencies>
    </AXCP_Rule>
  </Definition>
</Rule>

