PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/intermine/api/main/src/org/intermine/api/xml/ProfileHandler.java

https://gitlab.com/jsr38/intermine
Java | 241 lines | 161 code | 14 blank | 66 comment | 34 complexity | 7fc9a6d40f5c784f0b5c15b05e9116e2 MD5 | raw file
  1. package org.intermine.api.xml;
  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.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.HashSet;
  14. import java.util.LinkedHashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.Set;
  18. import org.intermine.api.bag.InvitationHandler;
  19. import org.intermine.api.profile.BagSet;
  20. import org.intermine.api.profile.BagValue;
  21. import org.intermine.api.profile.InterMineBag;
  22. import org.intermine.api.profile.InvalidBag;
  23. import org.intermine.api.profile.PreferencesHandler;
  24. import org.intermine.api.profile.Profile;
  25. import org.intermine.api.profile.ProfileManager;
  26. import org.intermine.api.profile.SavedQuery;
  27. import org.intermine.api.profile.TagHandler;
  28. import org.intermine.api.template.TemplateHelper;
  29. import org.intermine.model.userprofile.Tag;
  30. import org.intermine.objectstore.ObjectStoreWriter;
  31. import org.intermine.template.TemplateQuery;
  32. import org.intermine.template.xml.TemplateQueryHandler;
  33. import org.xml.sax.Attributes;
  34. import org.xml.sax.SAXException;
  35. import org.xml.sax.helpers.DefaultHandler;
  36. /**
  37. * Extension of DefaultHandler to handle parsing Profiles
  38. *
  39. * @author Kim Rutherford
  40. * @author dbutano
  41. */
  42. class ProfileHandler extends DefaultHandler
  43. {
  44. private ProfileManager profileManager;
  45. private String username;
  46. private String password;
  47. private Map<String, SavedQuery> savedQueries;
  48. private Map<String, InterMineBag> savedBags;
  49. private Map<String, List> sharedBagsByUser;
  50. private List<Map<String, String>> sharedBags;
  51. private Map<String, InvalidBag> invalidBags;
  52. private Map<String, TemplateQuery> savedTemplates = new HashMap<String, TemplateQuery>();
  53. private Set<Tag> tags;
  54. private Map<String, Set<BagValue>> bagsValues;
  55. private final Map<String, String> preferences = new HashMap<String, String>();
  56. private ObjectStoreWriter osw;
  57. private int version;
  58. private String apiKey = null;
  59. private boolean isLocal = true;
  60. private boolean isSuperUser;
  61. private final InvitationHandler invitationHandler = new InvitationHandler();
  62. /**
  63. * Constructor.
  64. *
  65. * @return invitation handler
  66. */
  67. public InvitationHandler getInvitationHandler() {
  68. return invitationHandler;
  69. }
  70. /**
  71. * The current child handler. If we have just seen a "bags" element, it will be an
  72. * InterMineBagBinding.InterMineBagHandler. If "template-queries" it will be an
  73. * TemplateQueryBinding.TemplateQueryHandler. If "queries" it will be a
  74. * PathQueryBinding.PathQueryHandler. If subHandler is not null subHandler.startElement() and
  75. * subHandler.endElement(), etc will be called from this class.
  76. */
  77. DefaultHandler subHandler = null;
  78. /**
  79. * Create a new ProfileHandler
  80. * @param profileManager the ProfileManager to pass to the Profile constructor
  81. * @param osw an ObjectStoreWriter to the production database, to write bags
  82. * @param version the version of the profile xml, an attribute on the profile manager xml
  83. * @param sharedBagsByUser list of bags shared by user
  84. */
  85. public ProfileHandler(ProfileManager profileManager, ObjectStoreWriter osw,
  86. int version, Map<String, List> sharedBagsByUser) {
  87. this(profileManager, null, null, new HashSet(), osw, version, sharedBagsByUser);
  88. }
  89. /**
  90. * Create a new ProfileHandler
  91. * @param profileManager the ProfileManager to pass to the Profile constructor
  92. * @param defaultUsername default username
  93. * @param defaultPassword default password
  94. * @param tags a set to populate with user tags
  95. * @param osw an ObjectStoreWriter to the production database, to write bags
  96. * @param version the version of the profile xml, an attribute on the profile manager xml
  97. * @param sharedBagsByUser list of bags shared by user
  98. */
  99. public ProfileHandler(ProfileManager profileManager, String defaultUsername,
  100. String defaultPassword, Set<Tag> tags, ObjectStoreWriter osw, int version,
  101. Map<String, List> sharedBagsByUser) {
  102. super();
  103. this.profileManager = profileManager;
  104. this.username = defaultUsername;
  105. this.password = defaultPassword;
  106. this.tags = tags;
  107. this.osw = osw;
  108. this.version = version;
  109. this.sharedBagsByUser = sharedBagsByUser;
  110. }
  111. /**
  112. * Return the de-serialised Profile.
  113. * @return the new Profile
  114. * @throws SAXException if error reading XML
  115. */
  116. public Profile getProfile() throws SAXException {
  117. Profile retval = new Profile(profileManager, username, null, password, savedQueries,
  118. new BagSet(savedBags, invalidBags),
  119. TemplateHelper.upcast(savedTemplates), apiKey,
  120. isLocal, isSuperUser);
  121. try {
  122. retval.getPreferences().putAll(preferences);
  123. } catch (RuntimeException e) {
  124. throw new SAXException(e);
  125. }
  126. return retval;
  127. }
  128. /**
  129. * Return a set of Tag objects to add to the Profile.
  130. * @return the set Tags
  131. */
  132. public Set<Tag> getTags() {
  133. return tags;
  134. }
  135. /**
  136. * Return a map of bag values for each bag.
  137. * @return the map of bag values
  138. */
  139. public Map<String, Set<BagValue>> getBagsValues() {
  140. return bagsValues;
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. @Override
  146. public void startElement(String uri, String localName, String qName, Attributes attrs)
  147. throws SAXException {
  148. if ("userprofile".equals(qName)) {
  149. if (attrs.getValue("username") != null) {
  150. username = attrs.getValue("username");
  151. }
  152. if (attrs.getValue("password") != null) {
  153. password = attrs.getValue("password");
  154. }
  155. if (attrs.getValue("apikey") != null) {
  156. apiKey = attrs.getValue("apikey");
  157. }
  158. if (attrs.getValue("localAccount") != null) {
  159. isLocal = Boolean.parseBoolean(attrs.getValue("localAccount"));
  160. }
  161. if (attrs.getValue("superUser") != null) {
  162. isSuperUser = Boolean.parseBoolean(attrs.getValue("superUser"));
  163. }
  164. }
  165. if ("bags".equals(qName)) {
  166. savedBags = new LinkedHashMap();
  167. invalidBags = new LinkedHashMap();
  168. bagsValues = new LinkedHashMap();
  169. subHandler = new InterMineBagHandler(
  170. profileManager.getProfileObjectStoreWriter(), osw,
  171. savedBags, invalidBags, bagsValues);
  172. }
  173. if ("shared-bags".equals(qName)) {
  174. sharedBags = new ArrayList<Map<String, String>>();
  175. subHandler = new SharedBagHandler(sharedBags);
  176. }
  177. if ("template-queries".equals(qName)) {
  178. savedTemplates = new LinkedHashMap();
  179. subHandler = new TemplateQueryHandler(savedTemplates, version);
  180. }
  181. if ("queries".equals(qName)) {
  182. savedQueries = new LinkedHashMap();
  183. subHandler = new SavedQueryHandler(savedQueries, savedBags, version);
  184. }
  185. if ("tags".equals(qName)) {
  186. subHandler = new TagHandler(username, tags);
  187. }
  188. if ("preferences".equals(qName)) {
  189. subHandler = new PreferencesHandler(preferences);
  190. }
  191. if ("invitations".equals(qName)) {
  192. subHandler = invitationHandler;
  193. }
  194. if (subHandler != null) {
  195. subHandler.startElement(uri, localName, qName, attrs);
  196. }
  197. }
  198. /**
  199. * {@inheritDoc}
  200. */
  201. @Override
  202. public void endElement(String uri, String localName, String qName) throws SAXException {
  203. super.endElement(uri, localName, qName);
  204. if ("bags".equals(qName) || ("template-queries").equals(qName)
  205. || "queries".equals(qName) || "items".equals(qName) || "tags".equals(qName)
  206. || "preferences".equals(qName) || "invitations".equals(qName)) {
  207. subHandler = null;
  208. }
  209. if ("shared-bags".equals(qName)) {
  210. if (sharedBagsByUser != null) {
  211. sharedBagsByUser.put(username, sharedBags);
  212. }
  213. }
  214. if (subHandler != null) {
  215. subHandler.endElement(uri, localName, qName);
  216. }
  217. }
  218. /**
  219. * {@inheritDoc}
  220. */
  221. @Override
  222. public void characters(char[] ch, int start, int length) throws SAXException {
  223. if (subHandler != null) {
  224. subHandler.characters(ch, start, length);
  225. }
  226. }
  227. }