1. Atlassian Tutorials
  2. Untitled project
  3. writing a macro using JSON

Commits

Reed Johnson  committed bd252f3

Committing everything I think

  • Participants
  • Branches default

Comments (0)

Files changed (10)

File README.txt

View file
  • Ignore whitespace
+You have successfully created a plugin using the Confluence plugin archetype!
+
+Here are the SDK commands you'll use immediately:
+
+* atlas-run   -- installs this plugin into Confluence and starts it on http://<machinename>:1990/confluence
+* atlas-debug -- same as atlas-run, but allows a debugger to attach at port 5005
+* atlas-cli   -- after atlas-run or atlas-debug, opens a Maven command line window:
+                 - 'pi' reinstalls the plugin into the running Confluence instance
+* atlas-help  -- prints description for all commands in the SDK
+
+Full documentation is always available at:
+
+https://developer.atlassian.com/display/DOCS/Developing+with+the+Atlassian+Plugin+SDK

File pom.xml

View file
  • Ignore whitespace
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.some.id</groupId>
+    <artifactId>examplemacro</artifactId>
+    <version>1.0</version>
+    <organization>
+        <name>Example Company</name>
+        <url>http://www.example.com/</url>
+    </organization>
+    <name>examplemacro</name>
+    <description>This plugin illustrates how to use JSON in a macro</description>
+    <packaging>atlassian-plugin</packaging>
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.6</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.confluence</groupId>
+            <artifactId>confluence</artifactId>
+            <version>${confluence.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.confluence.plugin</groupId>
+            <artifactId>func-test</artifactId>
+            <version>2.3</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>net.sourceforge.jwebunit</groupId>
+            <artifactId>jwebunit-htmlunit-plugin</artifactId>
+            <version>2.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>net.sourceforge.nekohtml</groupId>
+            <artifactId>nekohtml</artifactId>
+            <version>1.9.12</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.8.5</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>com.atlassian.maven.plugins</groupId>
+                <artifactId>maven-confluence-plugin</artifactId>
+                <version>${amps.version}</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <productVersion>${confluence.version}</productVersion>
+                    <productDataVersion>${confluence.data.version}</productDataVersion>
+                    <instructions/>
+                </configuration>
+            </plugin>
+            <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.6</source>
+                    <target>1.6</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    <properties>
+        <confluence.version>4.3</confluence.version>
+        <confluence.data.version>4.3</confluence.data.version>
+        <amps.version>3.11</amps.version>
+    </properties>
+</project>

File src/main/java/come/some/id/ExampleMacro.java

View file
  • Ignore whitespace
+package com.some.id;
+
+import java.util.Map;
+import java.util.List;
+
+import com.atlassian.confluence.content.render.xhtml.ConversionContext;
+import com.atlassian.confluence.macro.Macro;
+import com.atlassian.confluence.macro.MacroExecutionException;
+import com.atlassian.confluence.pages.PageManager;
+import com.atlassian.confluence.pages.Page;
+import com.atlassian.confluence.spaces.SpaceManager;
+import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
+import com.atlassian.user.User;
+import com.opensymphony.util.TextUtils;
+ 
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import com.atlassian.confluence.json.json.JsonArray;
+import com.atlassian.confluence.json.json.JsonObject;
+import com.atlassian.confluence.renderer.radeox.macros.MacroUtils;
+import com.atlassian.confluence.util.GeneralUtil;
+import com.atlassian.confluence.util.velocity.VelocityUtils;
+import org.apache.velocity.VelocityContext;
+ 
+
+
+public class ExampleMacro implements Macro
+{
+    private final PageManager pageManager;
+    private final SpaceManager spaceManager;
+
+    public ExampleMacro(PageManager pageManager, SpaceManager spaceManager)
+    {
+        this.pageManager = pageManager;
+        this.spaceManager = spaceManager;
+    }
+
+    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
+    DateFormat dayFormat = new SimpleDateFormat("EEEE");
+    private static final String DAY_DATE_JSON = "dayDateJson";
+    private static final String TEMPLATE = "examplemacro.vm";
+
+    @Override
+    public BodyType getBodyType()
+    {
+        return BodyType.NONE;
+    }
+ 
+    @Override
+    public OutputType getOutputType()
+    {
+        return OutputType.BLOCK;
+    }
+
+
+    @Override
+    public String execute(Map<String, String> params, String body, ConversionContext conversionContext) throws MacroExecutionException
+    {
+	Date startDate = getStartDateFromParams(params);
+        JsonObject dayDateJsonObject = new JsonObject();
+        JsonArray dayDateJsonArray = new JsonArray();
+	for (int i=0; i < 7; i++)
+	{
+	    Calendar cal = Calendar.getInstance();
+	    cal.setTime(startDate);
+	    cal.add(Calendar.DATE, i );
+	    Date date = cal.getTime();
+	    String day = dayFormat.format(date);
+	    dayDateJsonArray.add(nextDayDateJsonObject(day, date));
+	}
+	
+	dayDateJsonObject.setProperty("daysdates", dayDateJsonArray);
+	VelocityContext contextMap = new VelocityContext(MacroUtils.defaultVelocityContext());
+	contextMap.put(DAY_DATE_JSON, GeneralUtil.urlEncode(dayDateJsonObject.serialize()));
+	return VelocityUtils.getRenderedTemplate(TEMPLATE, contextMap);
+    }
+    
+    private Date getStartDateFromParams(Map params) throws MacroExecutionException
+    {
+	Date startDate = new Date();
+	if (params.size() > 1)
+	{
+	    try
+	    {
+		startDate = dateFormat.parse((String) params.get("date"));
+	    }
+	    catch (ParseException e)
+	    {
+		throw new MacroExecutionException("Unable to parse date");
+	    }
+	}
+	return startDate;
+    }
+
+    private JsonObject nextDayDateJsonObject(String day, Date date)
+    {
+	JsonObject nextDayDateJsonObject = new JsonObject();
+	nextDayDateJsonObject.setProperty("date",date);
+	nextDayDateJsonObject.setProperty("day", day);
+	return nextDayDateJsonObject;
+    }
+}

File src/main/resources/atlassian-plugin.xml

View file
  • Ignore whitespace
+<atlassian-plugin key="${project.groupId}.${project.artifactId}" name="${project.artifactId}" plugins-version="2">
+  <plugin-info>
+    <description>${project.description}</description>
+    <version>${project.version}</version>
+    <vendor name="${project.organization.name}" url="${project.organization.url}"/>
+  </plugin-info>
+  <web-resource key="resources" name="exampleMacro resources">
+    <resource type="velocity" name="template" location="examplemacro.vm"/>
+    <resource type="download" name="examplemacro.js" location="examplemacro.js"/>
+  </web-resource>
+  <xhtml-macro name="examplemacro" class="com.some.id.ExampleMacro" key="my-macro">
+    <parameters>
+      <parameter name="date" type="string"/>
+    </parameters>
+  </xhtml-macro>
+</atlassian-plugin>

File src/main/resources/com/some/id/examplemacro.properties

View file
  • Ignore whitespace
+
+example-macro.name=exampleMacro
+example-macro.description=The exampleMacro Plugin

File src/main/resources/examplemacro.js

View file
  • Ignore whitespace
+jQuery(function ($) {
+    var initExampleMacro = function ()
+    {
+        $(".exampleMacro").each(function()
+        {
+        var dayDates = $(this).find("input.dayDates").val();
+        var json;
+ 
+        // determine if the browser has native JSON parser support & create JSON object
+        if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
+        {
+            json = JSON.parse(decodeURIComponent(dayDates).replace(/\+/g, '\u00a0'));
+        } else {
+            json = eval('(' + decodeURIComponent(dayDates).replace(/\+/g, '\u00a0') + ')');
+        }
+ 
+        // create table
+        var html = "<table border=\"1\"><tr><th>DAY</th><th>DATE</th></tr>";
+        for (var i=0; i<7; i++)
+        {
+            html = html + "<tr><td>" + json.daysdates[i].day + "</td><td>" + json.daysdates[i].date + "</td></tr>";
+        }
+        html = html + "</table>";
+        $(this).html(html);
+    });
+};
+ 
+$(document).ready(function()
+    {
+        initExampleMacro();
+    });
+});

File src/main/resources/examplemacro.vm

View file
  • Ignore whitespace
+#requireResource("confluence.web.resources:jquery")
+#requireResource("com.some.id.examplemacro:resources")
+
+<div class="exampleMacro">
+	<fieldset class="parameters hidden">
+			<input type="hidden" class="dayDates" value="$dayDateJson">
+	</fieldset>
+</div>

File src/test/java/come/some/id/ExampleMacroTest.java

View file
  • Ignore whitespace
+package come.some.id;
+
+import org.junit.Test;
+
+/**
+ * Testing {@link come.some.id.ExampleMacro}
+ */
+public class ExampleMacroTest
+{
+    @Test
+    public void basic()
+    {
+        // add test here...
+    }
+}

File src/test/java/it/AbstractIntegrationTestCase.java

View file
  • Ignore whitespace
+package it;
+
+import com.atlassian.confluence.plugin.functest.AbstractConfluencePluginWebTestCase;
+import com.atlassian.confluence.plugin.functest.JWebUnitConfluenceWebTester;
+import com.atlassian.confluence.plugin.functest.TesterConfiguration;
+import junit.framework.Assert;
+
+import java.io.IOException;
+import java.util.Properties;
+
+public class AbstractIntegrationTestCase extends AbstractConfluencePluginWebTestCase
+{
+    @Override
+    protected JWebUnitConfluenceWebTester createConfluenceWebTester()
+    {
+        Properties props = new Properties();
+        props.put("confluence.webapp.protocol", "http");
+        props.put("confluence.webapp.host", "localhost");
+
+        // this is deceiving: the func test library checks for the system properties
+        // *before* checking in this properties file for these values, so these
+        // properties are technically ignored
+        props.put("confluence.webapp.port", Integer.parseInt(System.getProperty("http.port")));
+        props.put("confluence.webapp.context.path", System.getProperty("context.path"));
+
+        props.put("confluence.auth.admin.username", "admin");
+        props.put("confluence.auth.admin.password", "admin");
+
+        TesterConfiguration conf;
+        try
+        {
+            conf = new TesterConfiguration(props);
+        }
+        catch (IOException ioe)
+        {
+            Assert.fail("Unable to create tester: " + ioe.getMessage());
+            return null;
+        }
+
+        JWebUnitConfluenceWebTester tester = new JWebUnitConfluenceWebTester(conf);
+
+        tester.getTestContext().setBaseUrl(tester.getBaseUrl());
+        tester.setScriptingEnabled(false);
+
+        return tester;
+    }
+}

File src/test/java/it/IntegrationTestMyPlugin.java

View file
  • Ignore whitespace
+package it;
+
+public class IntegrationTestMyPlugin extends AbstractIntegrationTestCase
+{
+	public void testSomething()
+	{
+        gotoPage("");
+        assertTextPresent("Welcome");
+	}
+}