Concept:Commit/Java

From BITPlan Wiki
Revision as of 11:39, 7 February 2020 by Wf (talk | contribs) (created by WikiTask 2020-02-07T09:39:06Z)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

java code

@// This is a rythm template
@// the args are the standard wikiTask arguments
@import org.sidif.triple.TripleQuery
@import org.sidif.triple.Triple
@import com.alibaba.fastjson.JSON
@args() {
  String title 
  String logo
  org.sidif.wiki.WikiTask wikiTask
  org.sidif.triple.TripleStore tripleStore
}


@def static {

  /**
   * Base class
   */
  static abstract class TopicBase {
    // each Topic has a pageid - for non subobject thats the pagename
    public String pageid;

    /**
     * get a WikiSon version of the given name value
     * 
     * @param name
     * @param value
     * @return - the string representation
     */
    public String toWikiSon(String name, String value) {
      String result = "<!-- " + name + " is null-->\n";
      if (value != null)
        result = "|" + name + "=" + value + "\n";
      return result;
    }

    /**
     * get the SiDIF representation of the given property
     * 
     * @param name - the name of the property
     * @param value - the value of the property
     * @param type - the type of the property
     * @return - the SiDIF Sting representation of the property
     */
    public static String propertySiDIF(String name, String value, String type) {
      // default is a comment line which can be filled by uncommenting
      String result = String.format("# is is %s of it\n",name);;
      // if the value is not empty
      if ((value != null) && (!("".equals(value.trim())))) {
        // do we need to quote the result?
        String quote = "";
        // this depends on the Type
        if ("Text".equals(type)) {
          quote = "\"";
        }
        // create a SIDIF Property line like
        // "John" is lastname of it
        // convert double quotes to single quotes - FIXME - should we escape instead?
        value=value.replace("\"","'");
        result = String.format("%s%s%s is %s of it\n",quote,value,quote,name);
      }
      // return the SiDIF property line
      return result;
    }

    /**
     * get me as a String
     * 
     * @param name
     * @param value
     * @return
     */
    public static String propertySiDIF(String name, String value) {
      String result = propertySiDIF(name, value, "Text");
      return result;
    }

    /**
     * check if the given boolean String value is true
     * 
     * @param value
     * @return true if the value is not null and has true/TRUE as it's string
     *         content
     */
    public boolean isTrue(String value) {
      boolean result = false;
      if (value != null && value.toLowerCase().equals("true")) {
        result = true;
      }
      return result;
    }

    /**
     * initialize
     */
    public void init(TripleQuery query) {
    }
  } // TopicBase
 /**
  * Commit
  * A Git commit
  */
  public static class Commit extends TopicBase {
  
    public String host;
    public String path;
    public String project;
    public String subject;
    public String name;
    public String date;
    public String hash;

    public String getHost() { return host; }
    public void setHost(String pHost) { host=pHost; }
    public String getPath() { return path; }
    public void setPath(String pPath) { path=pPath; }
    public String getProject() { return project; }
    public void setProject(String pProject) { project=pProject; }
    public String getSubject() { return subject; }
    public void setSubject(String pSubject) { subject=pSubject; }
    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getDate() { return date; }
    public void setDate(String pDate) { date=pDate; }
    public String getHash() { return hash; }
    public void setHash(String pHash) { hash=pHash; }
    /**
     * convert this Commit to a JSON string
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this Commit to a WikiSon string
     * @return the WikiSon representation of this Commit
     */
    public String toWikiSon() {
      String wikison= "{{Commit\n";
      wikison+=toWikiSon("host",host);
      wikison+=toWikiSon("path",path);
      wikison+=toWikiSon("project",project);
      wikison+=toWikiSon("subject",subject);
      wikison+=toWikiSon("name",name);
      wikison+=toWikiSon("date",date);
      wikison+=toWikiSon("hash",hash);
      wikison+="}}\n";
      return wikison;
    }

    /**
     * convert this Commit to a SiDIF string
     * @return the SiDIF representation of this Commit
     */
    public String toSiDIF() {
      String siDIF = String.format("%s isA Commit\n",this.pageid);
      siDIF+=propertySiDIF("host",host,"URL");
      siDIF+=propertySiDIF("path",path,"Text");
      siDIF+=propertySiDIF("project",project,"Text");
      siDIF+=propertySiDIF("subject",subject,"Text");
      siDIF+=propertySiDIF("name",name,"Text");
      siDIF+=propertySiDIF("date",date,"Date");
      siDIF+=propertySiDIF("hash",hash,"Text");
      return siDIF;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

    /**
     * default constructor for Commit
     */
    public Commit() {}

    /**
     * construct a Commit from the given Triple
     * @param query - the TripleQuery to get the triples from
     * @param pCommitTriple - the triple to construct me from
     */
    public Commit(TripleQuery query,Triple pCommitTriple) {
      this(query,pCommitTriple.getSubject().toString());
    } // constructor

    /**
     * construct a Commit from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public Commit(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple hostTriple=query.selectSingle(pageid,"host",null);
      if (hostTriple==null)
        hostTriple=query.selectSingle(pageid,"Property:Commit_host",null);
      if (hostTriple!=null) 
        host=hostTriple.getObject().toString();
      Triple pathTriple=query.selectSingle(pageid,"path",null);
      if (pathTriple==null)
        pathTriple=query.selectSingle(pageid,"Property:Commit_path",null);
      if (pathTriple!=null) 
        path=pathTriple.getObject().toString();
      Triple projectTriple=query.selectSingle(pageid,"project",null);
      if (projectTriple==null)
        projectTriple=query.selectSingle(pageid,"Property:Commit_project",null);
      if (projectTriple!=null) 
        project=projectTriple.getObject().toString();
      Triple subjectTriple=query.selectSingle(pageid,"subject",null);
      if (subjectTriple==null)
        subjectTriple=query.selectSingle(pageid,"Property:Commit_subject",null);
      if (subjectTriple!=null) 
        subject=subjectTriple.getObject().toString();
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:Commit_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple dateTriple=query.selectSingle(pageid,"date",null);
      if (dateTriple==null)
        dateTriple=query.selectSingle(pageid,"Property:Commit_date",null);
      if (dateTriple!=null) 
        date=dateTriple.getObject().toString();
      Triple hashTriple=query.selectSingle(pageid,"hash",null);
      if (hashTriple==null)
        hashTriple=query.selectSingle(pageid,"Property:Commit_hash",null);
      if (hashTriple!=null) 
        hash=hashTriple.getObject().toString();
      init(query);
    } // constructor for Commit
    
    // >>>{user defined topic code}{Commit}{Commit}
    // <<<{user defined topic code}{Commit}{Commit}
  } // class Commit
  /**
   * Manager for Commit
   */
  public static class CommitManager extends TopicBase {
 
    public String topicName="Commit";
    public transient List<Commit> mCommits=new ArrayList<Commit>();
    public transient Map<String,Commit> mCommitMap=new LinkedHashMap<String,Commit>();

    /**
     * get my Commits
     */
    public List<Commit> getCommits() {
      List<Commit> result=this.mCommits;
      return result;
    }

    /**
     *  add a new Commit 
     */
    public Commit add(Commit pCommit) {
      mCommits.add(pCommit);
      mCommitMap.put(pCommit.getPageid(),pCommit);
      return pCommit;
    }

    /**
     *  add a new Commit from the given triple
     */
    public Commit add(TripleQuery query,Triple pCommitTriple) {
      Commit lCommit=new Commit(query,pCommitTriple);
      add(lCommit);
      return lCommit;
    }

    // reinitialize my mCommit map
    public void reinit() {
      mCommitMap.clear();
      for (Commit lCommit:mCommits) {
        mCommitMap.put(lCommit.getPageid(),lCommit);
      }
    }

    // convert this manager to json format 
    public String toJson() { return JSON.toJSONString(this); }
    
    // get a new manager from the given json string
    public static CommitManager fromJson(String json) {
      CommitManager result=JSON.parseObject(json, CommitManager.class);
      result.reinit();
      return result;
    }

    // default constructor for Commit Manager
    public CommitManager() {}

    // add Commits from the given query
    public void addCommits(TripleQuery pCommitQuery,TripleQuery query) {
      if (pCommitQuery!=null) {
        for (Triple lCommitTriple:pCommitQuery.getTriples()) {
          add(query,lCommitTriple);
        }
      }
    }

    // construct me from the given triple Query query
    public CommitManager(TripleQuery query) {
      // first query the SiDIF bases triplestore
      TripleQuery lCommitQuery=query.query(null,"isA","Commit");
      addCommits(lCommitQuery,query);
      // then the SMW triplestore
      lCommitQuery=query.query(null,"Property:IsA","Commit");
      addCommits(lCommitQuery,query);
      init(query);
    } // constructor for Commit Manager
    
    // >>>{user defined topicmanager code}{Commit}{Commit}
    // <<<{user defined topicmanager code}{Commit}{Commit}
  } // class Commit Manager

}
Showing below 0 pages.

Retrieved from "https://wiki.bitplan.com/index.php?title=Concept:Commit/Java&oldid=11592#smw-result"