PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/com/atlassian/connect/play/java/model/AcHostModel.java

https://bitbucket.org/awei/ac-play-java
Java | 128 lines | 104 code | 21 blank | 3 comment | 2 complexity | 37c72287c937580ed41178c4bb4c9cd5 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.connect.play.java.model;
  2. import com.atlassian.connect.play.java.AcHost;
  3. import com.atlassian.fugue.Option;
  4. import play.db.jpa.JPA;
  5. import java.util.List;
  6. import javax.persistence.Column;
  7. import javax.persistence.Entity;
  8. import javax.persistence.GeneratedValue;
  9. import javax.persistence.Id;
  10. import javax.persistence.NamedQueries;
  11. import javax.persistence.NamedQuery;
  12. import javax.persistence.SequenceGenerator;
  13. import javax.persistence.Table;
  14. import static com.atlassian.fugue.Option.none;
  15. import static com.atlassian.fugue.Option.option;
  16. import static play.data.validation.Constraints.MaxLength;
  17. import static play.data.validation.Constraints.Required;
  18. /**
  19. * This represents the host application of the remote app plugin
  20. */
  21. @Entity
  22. @Table (name = "ac_host")
  23. @NamedQueries ({
  24. @NamedQuery (name = "AcHostModel.findAll", query = "SELECT a FROM AcHostModel a"),
  25. @NamedQuery (name = "AcHostModel.findByKey", query = "SELECT a FROM AcHostModel a where a.key = :key"),
  26. @NamedQuery (name = "AcHostModel.findByUrl", query = "SELECT a FROM AcHostModel a where a.baseUrl = :baseUrl")
  27. })
  28. public final class AcHostModel implements AcHost
  29. {
  30. @Id
  31. @SequenceGenerator (name = "ac_host_gen", sequenceName = "ac_host_seq")
  32. @GeneratedValue (generator = "ac_host_gen")
  33. public Long id;
  34. @Required
  35. @Column (unique = true, nullable = false)
  36. public String key;
  37. @Required
  38. @MaxLength (512)
  39. @Column (nullable = false, length = 512)
  40. public String publicKey;
  41. @Required
  42. @MaxLength (512)
  43. @Column (unique = true, nullable = false, length = 512)
  44. public String baseUrl;
  45. public String name;
  46. public String description;
  47. public Long getId()
  48. {
  49. return id;
  50. }
  51. public String getKey()
  52. {
  53. return key;
  54. }
  55. public String getName()
  56. {
  57. return name;
  58. }
  59. public String getDescription()
  60. {
  61. return description;
  62. }
  63. @Override
  64. public String getBaseUrl()
  65. {
  66. return baseUrl;
  67. }
  68. @Override
  69. public String getPublicKey()
  70. {
  71. return publicKey;
  72. }
  73. public String getConsumerInfoUrl()
  74. {
  75. return baseUrl + "/plugins/servlet/oauth/consumer-info";
  76. }
  77. public static List<AcHostModel> all()
  78. {
  79. return JPA.em().createNamedQuery("AcHostModel.findAll", AcHostModel.class).getResultList();
  80. }
  81. public static Option<AcHostModel> findByKey(String key)
  82. {
  83. final List<AcHostModel> resultList = JPA.em().createNamedQuery("AcHostModel.findByKey", AcHostModel.class).
  84. setParameter("key", key).
  85. getResultList();
  86. return resultList.isEmpty() ? none(AcHostModel.class) : option(resultList.get(0));
  87. }
  88. public static Option<AcHostModel> findByUrl(String baseUrl)
  89. {
  90. final List<AcHostModel> resultList = JPA.em().createNamedQuery("AcHostModel.findByUrl", AcHostModel.class).
  91. setParameter("baseUrl", baseUrl).
  92. getResultList();
  93. return resultList.isEmpty() ? none(AcHostModel.class) : option(resultList.get(0));
  94. }
  95. public static void create(AcHostModel hostModel)
  96. {
  97. JPA.em().persist(hostModel);
  98. }
  99. public static void delete(Long id)
  100. {
  101. final AcHostModel acHostModel = JPA.em().find(AcHostModel.class, id);
  102. if (acHostModel != null)
  103. {
  104. JPA.em().remove(acHostModel);
  105. }
  106. }
  107. }