/intermine/api/main/src/org/intermine/api/query/codegen/WebserviceCodeGenInfo.java

https://gitlab.com/jsr38/intermine · Java · 204 lines · 95 code · 20 blank · 89 comment · 8 complexity · eeff1cbcf3f12fc21a0ad14c9171a5ca MD5 · raw file

  1. package org.intermine.api.query.codegen;
  2. /*
  3. * Copyright (C) 2002-2016 FlyMine
  4. *
  5. * This code may be freely distributed and modified under the
  6. * terms of the GNU Lesser General Public Licence. This should
  7. * be distributed with the code. See the LICENSE file for more
  8. * information or http://www.gnu.org/copyleft/lesser.html.
  9. *
  10. */
  11. import java.util.Properties;
  12. import org.apache.commons.lang.StringUtils;
  13. import org.intermine.api.profile.Profile;
  14. import org.intermine.api.util.AnonProfile;
  15. import org.intermine.pathquery.PathQuery;
  16. import org.intermine.template.TemplateQuery;
  17. /**
  18. * Class to represent the information used to generate web service source code.
  19. *
  20. * @author Fengyuan Hu
  21. *
  22. */
  23. public class WebserviceCodeGenInfo
  24. {
  25. private PathQuery query;
  26. private String serviceBaseURL;
  27. private String projectTitle;
  28. private String perlWSModuleVer;
  29. private boolean isPublic;
  30. private Profile user;
  31. private String resultTablesLib = null;
  32. private String baseUrl = null;
  33. private String lineBreak;
  34. private Properties properties = new Properties();
  35. /**
  36. * Constructor.
  37. *
  38. * @param query a PathQuery to copy
  39. * @param serviceBaseURL the base url of web service
  40. * @param projectTitle the Title of a local InterMine project
  41. * @param perlWSModuleVer the perl web service module version on CPAN
  42. * @param isPubliclyAccessible whether this query can be accessed by the public
  43. * @param user the name of the user who was logged in when this info was generated
  44. *
  45. */
  46. public WebserviceCodeGenInfo(PathQuery query, String serviceBaseURL, String projectTitle,
  47. String perlWSModuleVer, boolean isPubliclyAccessible, Profile user) {
  48. this.query = query;
  49. this.serviceBaseURL = serviceBaseURL;
  50. this.projectTitle = projectTitle;
  51. this.perlWSModuleVer = perlWSModuleVer;
  52. this.isPublic = isPubliclyAccessible;
  53. this.user = user;
  54. this.lineBreak = System.getProperty("line.separator");
  55. }
  56. /**
  57. * @param pq a PathQuery to copy
  58. * @param serviceBaseURL the base url of web service
  59. * @param projectTitle the Title of a local InterMine project
  60. * @param perlWSModuleVer the perl web service module version on CPAN
  61. * @param pathQueryIsPublic whether this query can be accessed by the public
  62. * @param profile the profile of the user who was logged in when this info was generated
  63. * @param lineBreak which linebreak to use
  64. */
  65. public WebserviceCodeGenInfo(PathQuery pq, String serviceBaseURL,
  66. String projectTitle, String perlWSModuleVer,
  67. boolean pathQueryIsPublic, Profile profile, String lineBreak) {
  68. this.query = pq;
  69. this.serviceBaseURL = serviceBaseURL;
  70. this.projectTitle = projectTitle;
  71. this.perlWSModuleVer = perlWSModuleVer;
  72. this.isPublic = pathQueryIsPublic;
  73. this.user = profile;
  74. this.lineBreak = lineBreak;
  75. }
  76. /**
  77. * @param props properties
  78. */
  79. public void readWebProperties(Properties props) {
  80. this.properties.putAll(props);
  81. if (properties != null) {
  82. resultTablesLib = (String) properties.get("ws.imtables.provider");
  83. baseUrl = properties.get("webapp.baseurl") + "/" + properties.get("webapp.path") + "/";
  84. }
  85. }
  86. /**
  87. * Get a configured property for which an accessor does not exist.
  88. *
  89. * @param key The key for this property.
  90. * @param defaultValue The value to return if this property is not configured.
  91. * @return The value of the property.
  92. */
  93. public String getProperty(String key, String defaultValue) {
  94. return properties.getProperty(key, defaultValue);
  95. }
  96. /**
  97. *
  98. * @return results tables
  99. */
  100. public String getResultsTablesLib() {
  101. return resultTablesLib;
  102. }
  103. /**
  104. *
  105. * @return base URL
  106. */
  107. public String getBaseUrl() {
  108. return baseUrl;
  109. }
  110. /**
  111. * Returns the filename that should be associated with this query.
  112. * @return a file name
  113. */
  114. public String getFileName() {
  115. if (query instanceof TemplateQuery) {
  116. return "template_query";
  117. }
  118. return "query";
  119. }
  120. /**
  121. * @return the query
  122. */
  123. public PathQuery getQuery() {
  124. return query;
  125. }
  126. /**
  127. * @return the serviceBaseURL
  128. */
  129. public String getServiceBaseURL() {
  130. return serviceBaseURL;
  131. }
  132. /**
  133. * @return the projectTitle
  134. */
  135. public String getProjectTitle() {
  136. return projectTitle;
  137. }
  138. /**
  139. * @return the perlWSModuleVer
  140. */
  141. public String getPerlWSModuleVer() {
  142. return perlWSModuleVer;
  143. }
  144. /**
  145. * Returns whether this query is publicly accessible. If so,
  146. * then the webservice will not need to implement a log-in.
  147. * @return Whether the query is public.
  148. */
  149. public boolean isPublic() {
  150. return isPublic;
  151. }
  152. /**
  153. * The name of the user logged in when this info was generated
  154. * @return The name of the user
  155. */
  156. public String getUserName() {
  157. return user.getUsername();
  158. }
  159. /**
  160. * @return a line break
  161. */
  162. public String getLineBreak() {
  163. return lineBreak;
  164. }
  165. /**
  166. * A token for the user. The permanent token is preferred, but a temporary one
  167. * is generated if that is not available.
  168. * @return A token for the the user
  169. */
  170. public String getUserToken() {
  171. if (user.getApiKey() != null) {
  172. return user.getApiKey();
  173. }
  174. return user.getDayToken();
  175. }
  176. /**
  177. * @return True if this user has a permanent profile.
  178. */
  179. public boolean isLoggedIn() {
  180. return user.getUserId() != null && !(
  181. StringUtils.isBlank(user.getUsername())
  182. || AnonProfile.USERNAME.equals(user.getUsername()));
  183. }
  184. }