PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/content/server/framework/atlassian-sdk/store-and-retrieve-plugin-data.md

https://bitbucket.org/zchristmas/atlassian-sdk-docs
Markdown | 192 lines | 155 code | 37 blank | 0 comment | 0 complexity | 1994e6356596234b2df6dca33910717b MD5 | raw file
Possible License(s): LGPL-2.0
  1. ---
  2. aliases:
  3. - /server/framework/atlassian-sdk/store-and-retrieve-plugin-data-16352160.html
  4. - /server/framework/atlassian-sdk/store-and-retrieve-plugin-data-16352160.md
  5. category: devguide
  6. confluence_id: 16352160
  7. dac_edit_link: https://developer.atlassian.com/pages/editpage.action?cjm=wozere&pageId=16352160
  8. dac_view_link: https://developer.atlassian.com/pages/viewpage.action?cjm=wozere&pageId=16352160
  9. date: '2017-12-08'
  10. guides: tutorials
  11. legacy_title: Store and retrieve plugin data
  12. platform: server
  13. product: atlassian-sdk
  14. subcategory: learning
  15. title: Store and retrieve plugin data
  16. ---
  17. # Store and retrieve plugin data
  18. Your plugin now has a sophisticated look and feel. In this last section of the tutorial you'll make your plugin fully functional. You'll add a `POST` method to the admin.vm template so you can enter and retrieve user data.
  19. ## About plugin data storage
  20. When you added component import modules to your plugin, you added `PluginSettingsFactory`. This import allows you to use `PluginSettings`, a SAL service that manages your plugin data storage. The `POST` method you add to your admin.vm template will submit the form to the database, and `PluginSettings` permits you to retrieve stored submissions from the database.
  21. ## Step 1. Add a `POST` method to your admin.vm template
  22. This is one of the most common HTTP requests. In this step, you'll add a line of code to your admin.vm file.
  23. 1. Open your admin.vm file from `src/main/resources`.
  24. 2. Add a `POST` method in the `<form>` tag.
  25. ``` xml
  26. <html>
  27. <head>
  28. <title>MyServlet Admin</title>
  29. <meta name="decorator" content="atl.admin">
  30. </head>
  31. <body>
  32. <form id="admin" class="aui" action="" method="POST">
  33. <div class="field-group">
  34. <label for="name">Name:</label>
  35. <input type="text" id="name" name="name" class="text">
  36. </div>
  37. <div class="field-group">
  38. <label for="age">Age:</label>
  39. <input type="text" id="age" name="age" class="text">
  40. </div>
  41. <div class="field-group">
  42. <input type="submit" value="Save" class="button">
  43. </div>
  44. </form>
  45. </body>
  46. </html>
  47. ```
  48. 3. Save and close the file.
  49. ## Step 2. Update project files in Eclipse
  50. In this step you'll update the dependencies in your `pom.xml` and `MyPluginServlet` so you can store and retrieve data. 
  51. 1. Open `MyPluginServlet.java`.
  52. 2. Replace the contents of the class with the following.
  53. ``` java
  54. package com.atlassian.plugins.tutorial.refapp;
  55. import java.util.HashMap;
  56. import java.util.Map;
  57. import java.io.IOException;
  58. import java.net.URI;
  59. import javax.servlet.*;
  60. import javax.servlet.http.HttpServlet;
  61. import javax.servlet.http.HttpServletRequest;
  62. import javax.servlet.http.HttpServletResponse;
  63. import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
  64. import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
  65. import javax.inject.Inject;
  66. import com.atlassian.sal.api.auth.LoginUriProvider;
  67. import com.atlassian.sal.api.user.UserManager;
  68. import com.atlassian.templaterenderer.TemplateRenderer;
  69. import com.atlassian.sal.api.pluginsettings.PluginSettings;
  70. import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
  71.  
  72. @Scanned
  73. public class MyPluginServlet extends HttpServlet
  74. {
  75. private static final String PLUGIN_STORAGE_KEY = "com.atlassian.plugins.tutorial.refapp.adminui";
  76. @ComponentImport
  77. private final UserManager userManager;
  78. @ComponentImport
  79. private final LoginUriProvider loginUriProvider;
  80. @ComponentImport
  81. private final TemplateRenderer templateRenderer;
  82. @ComponentImport
  83. private final PluginSettingsFactory pluginSettingsFactory;
  84. @Inject
  85. public MyPluginServlet(UserManager userManager, LoginUriProvider loginUriProvider, TemplateRenderer templateRenderer, PluginSettingsFactory pluginSettingsFactory) {
  86. this.userManager = userManager;
  87. this.loginUriProvider = loginUriProvider;
  88. this.templateRenderer = templateRenderer;
  89. this.pluginSettingsFactory = pluginSettingsFactory;
  90. }
  91. @Override
  92. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  93. {
  94. String username = userManager.getRemoteUsername(request);
  95. if (username == null || !userManager.isSystemAdmin(username))
  96. {
  97. redirectToLogin(request, response);
  98. return;
  99. }
  100. Map<String, Object> context = new HashMap<String, Object>();
  101. PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings();
  102. if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".name") == null){
  103. String noName = "Enter a name here.";
  104. pluginSettings.put(PLUGIN_STORAGE_KEY +".name", noName);
  105. }
  106. if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".age") == null){
  107. String noAge = "Enter an age here.";
  108. pluginSettings.put(PLUGIN_STORAGE_KEY + ".age", noAge);
  109. }
  110. context.put("name", pluginSettings.get(PLUGIN_STORAGE_KEY + ".name"));
  111. context.put("age", pluginSettings.get(PLUGIN_STORAGE_KEY + ".age"));
  112. response.setContentType("text/html;charset=utf-8");
  113. templateRenderer.render("admin.vm", response.getWriter());
  114. }
  115. @Override
  116. protected void doPost(HttpServletRequest req, HttpServletResponse response)
  117. throws ServletException, IOException {
  118. PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings();
  119. pluginSettings.put(PLUGIN_STORAGE_KEY + ".name", req.getParameter("name"));
  120. pluginSettings.put(PLUGIN_STORAGE_KEY + ".age", req.getParameter("age"));
  121. response.sendRedirect("test");
  122. }
  123. private void redirectToLogin(HttpServletRequest request, HttpServletResponse response) throws IOException
  124. {
  125. response.sendRedirect(loginUriProvider.getLoginUri(getUri(request)).toASCIIString());
  126. }
  127. private URI getUri(HttpServletRequest request)
  128. {
  129. StringBuffer builder = request.getRequestURL();
  130. if (request.getQueryString() != null)
  131. {
  132. builder.append("?");
  133. builder.append(request.getQueryString());
  134. }
  135. return URI.create(builder.toString());
  136. }
  137. // This is what your MyPluginServlet.java should look like in its final stages.
  138. }
  139. ```
  140. 3. Save and close `MyPluginServlet`.
  141. ## Step 3. Enter and retrieve data 
  142. The final step is to verify that your plugin can store and retrieve data. Here you'll make an entry and confirm that you can look it up using the `PLUGIN_STORAGE_KEY`. You'll perform your lookups in the Plugin Data Console.
  143. 1. Reload your plugin and return to **<a href="http://localhost:2990/jira/plugins/servlet/test" class="uri external-link">localhost:2990/jira/plugins/servlet/test</a>**.
  144. 2. Make an entry such as Charlie/34 for Name/Age and click **Save**.
  145. 3. Navigate to the **Plugin Data Editor**. You can navigate directly to the editor at **<a href="http://localhost:2990/jira/plugins/servlet/pde" class="uri external-link">localhost:2990/jira/plugins/servlet/pde</a>**
  146. Alternatively you can click **Question Mark &gt; Developer Toolbox &gt; Plugin Data Console**.
  147. 4. Under Plugin Settings Query, choose **Global**.
  148. 5. For the Data Key, enter `com.atlassian.plugins.tutorial.refapp.adminui.name` to query the names entered, or `com.atlassian.plugins.tutorial.refapp.adminui.age` to query the ages entered.
  149. This is the `PLUGIN_STORAGE_KEY` value that you coded into your `MyPluginServlet` class.
  150. 6. Click **Lookup**.
  151. You'll see your entries displayed in the **Results** section.
  152. ## Next steps
  153. You've created a plugin complete with a GUI that accepts and stores data - no minor feat. You might consider learning more about [developer tools](https://developer.atlassian.com/display/DOCS/Developer+Tools), [creating plugin tests](https://developer.atlassian.com/display/DOCS/Writing+and+Running+Plugin+Tests), or [perusing the FAQ](https://developer.atlassian.com/display/DOCS/Plugin+Development+FAQ):  
  154. [Developing plugins for Confluence](https://developer.atlassian.com/display/CONFDEV/Confluence+Plugin+Guide)
  155. [Developing for the Marketplace](https://developer.atlassian.com/display/MARKET/Developing+for+the+Marketplace)