Advertisement
Guest User

Untitled

a guest
Oct 11th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. ItemResource Page
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. package org.openmrs.module.basicexample.web.controller;
  9.  
  10. import java.util.List;
  11. import org.openmrs.api.context.Context;
  12. import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource;
  13. import org.openmrs.module.basicexample.Item;
  14. import org.openmrs.module.basicexample.api.BasicexampleService;
  15. import org.openmrs.module.webservices.rest.web.RequestContext;
  16. import org.openmrs.module.webservices.rest.web.RestConstants;
  17. import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
  18. import org.openmrs.module.webservices.rest.web.annotation.Resource;
  19. import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
  20. import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
  21. import org.openmrs.module.webservices.rest.web.representation.Representation;
  22. import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
  23. import org.openmrs.module.webservices.rest.web.response.ResponseException;
  24. import org.openmrs.module.webservices.rest.web.resource.impl.DataDelegatingCrudResource;
  25. import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
  26. import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
  27.  
  28. @Resource(name = RestConstants.VERSION_1 + "/item", supportedClass = Item.class, supportedOpenmrsVersions = { "2.0.*",
  29. "2.1.*", "2.2.*", "2.3.*", "2.4.* " })
  30. public class ItemResource extends DataDelegatingCrudResource<Item> {
  31.  
  32. @Override
  33. public Item getByUniqueId(String string) {
  34. System.out.println("****************GET: ");
  35. List<Item> its = Context.getService(BasicexampleService.class).getAllItems();
  36.  
  37. Item it = new Item();
  38. return it;
  39. }
  40.  
  41. public NeedsPaging<Item> doGetAll(RequestContext context) {
  42. return new NeedsPaging<Item>(Context.getService(BasicexampleService.class).getAllItems(), context);
  43. }
  44.  
  45. @Override
  46. protected void delete(Item t, String string, RequestContext rc) throws ResponseException {
  47. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  48. }
  49.  
  50. @Override
  51. public void purge(Item t, RequestContext rc) throws ResponseException {
  52. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  53. }
  54.  
  55. @Override
  56. public DelegatingResourceDescription getRepresentationDescription(Representation r) {
  57. if (r instanceof DefaultRepresentation) {
  58. DelegatingResourceDescription description = new DelegatingResourceDescription();
  59. description.addProperty("uuid");
  60. description.addProperty("description");
  61. description.addProperty("owner");
  62. description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
  63. description.addSelfLink();
  64. return description;
  65. } else if (r instanceof FullRepresentation) {
  66. DelegatingResourceDescription description = new DelegatingResourceDescription();
  67. description.addProperty("uuid");
  68. description.addProperty("description");
  69. description.addProperty("owner");
  70. description.addSelfLink();
  71. return description;
  72. }
  73. return null;
  74. }
  75.  
  76. @Override
  77. public Item newDelegate() {
  78. return new Item();
  79. }
  80.  
  81. @Override
  82. public Item save(Item t) {
  83. Item item = Context.getService(BasicexampleService.class).saveItem(t);
  84. return item;
  85. }
  86.  
  87. @PropertyGetter("display")
  88. public String getDisplayString(Item item) {
  89. return item.getDescription();
  90. }
  91.  
  92. }
  93.  
  94.  
  95. //==========================================================================================================
  96.  
  97. /**
  98. * This Source Code Form is subject to the terms of the Mozilla Public License,
  99. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  100. * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
  101. * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
  102. *
  103. * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
  104. * graphic logo is a trademark of OpenMRS Inc.
  105. */
  106. package org.openmrs.module.basicexample;
  107.  
  108. import org.openmrs.BaseOpenmrsData;
  109. import org.openmrs.User;
  110.  
  111. import javax.persistence.Basic;
  112. import javax.persistence.Column;
  113. import javax.persistence.Entity;
  114. import javax.persistence.GeneratedValue;
  115. import javax.persistence.Id;
  116. import javax.persistence.JoinColumn;
  117. import javax.persistence.ManyToOne;
  118. import javax.persistence.Table;
  119.  
  120. /**
  121. * Please note that a corresponding table schema must be created in liquibase.xml.
  122. */
  123. //Uncomment 2 lines below if you want to make the Item class persistable, see also BasicexampleDaoTest and liquibase.xml
  124. @Entity(name = "basicexample.Item")
  125. @Table(name = "basicexample_item")
  126. public class Item extends BaseOpenmrsData {
  127.  
  128. @Id
  129. @GeneratedValue
  130. @Column(name = "basicexample_item_id")
  131. private Integer id;
  132.  
  133. @ManyToOne
  134. @JoinColumn(name = "owner")
  135. private User owner;
  136.  
  137. @Basic
  138. @Column(name = "description", length = 255)
  139. private String description;
  140.  
  141. @Override
  142. public Integer getId() {
  143. return id;
  144. }
  145.  
  146. @Override
  147. public void setId(Integer id) {
  148. this.id = id;
  149. }
  150.  
  151. @Override
  152. public String getUuid() {
  153. return super.getUuid();
  154. }
  155.  
  156. @Override
  157. public void setUuid(String uuid) {
  158. super.setUuid(uuid);
  159. }
  160.  
  161. public User getOwner() {
  162. return owner;
  163. }
  164.  
  165. public void setOwner(User owner) {
  166. this.owner = owner;
  167. }
  168.  
  169. public String getDescription() {
  170. return description;
  171. }
  172.  
  173. public void setDescription(String description) {
  174. this.description = description;
  175. }
  176. }
  177.  
  178.  
  179. //===============================================================================================================
  180.  
  181. /**
  182. * This Source Code Form is subject to the terms of the Mozilla Public License,
  183. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  184. * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
  185. * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
  186. *
  187. * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
  188. * graphic logo is a trademark of OpenMRS Inc.
  189. */
  190. package org.openmrs.module.basicexample.api.impl;
  191.  
  192. import java.util.List;
  193. import org.openmrs.api.APIException;
  194. import org.openmrs.api.UserService;
  195. import org.openmrs.api.impl.BaseOpenmrsService;
  196. import org.openmrs.module.basicexample.Item;
  197. import org.openmrs.module.basicexample.api.BasicexampleService;
  198. import org.openmrs.module.basicexample.api.dao.BasicexampleDao;
  199.  
  200. public class BasicexampleServiceImpl extends BaseOpenmrsService implements BasicexampleService {
  201.  
  202. BasicexampleDao dao;
  203.  
  204. UserService userService;
  205.  
  206. /**
  207. * Injected in moduleApplicationContext.xml
  208. */
  209. public void setDao(BasicexampleDao dao) {
  210. this.dao = dao;
  211. }
  212.  
  213. /**
  214. * Injected in moduleApplicationContext.xml
  215. */
  216. public void setUserService(UserService userService) {
  217. this.userService = userService;
  218. }
  219.  
  220. @Override
  221. public Item getItemByUuid(String uuid) throws APIException {
  222. return dao.getItemByUuid(uuid);
  223. }
  224.  
  225. @Override
  226. public List<Item> getAllItems() throws APIException {
  227. return dao.getAllItems();
  228. }
  229.  
  230. @Override
  231. public Item saveItem(Item item) throws APIException {
  232. if (item.getOwner() == null) {
  233. item.setOwner(userService.getUser(1));
  234. }
  235.  
  236. return dao.saveItem(item);
  237. }
  238. }
  239.  
  240.  
  241. //==========================================================================================================
  242.  
  243. /**
  244. * This Source Code Form is subject to the terms of the Mozilla Public License,
  245. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  246. * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
  247. * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
  248. *
  249. * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
  250. * graphic logo is a trademark of OpenMRS Inc.
  251. */
  252. package org.openmrs.module.basicexample.api.dao;
  253.  
  254. import java.util.List;
  255. import org.hibernate.Criteria;
  256. import org.hibernate.criterion.Restrictions;
  257. import org.openmrs.api.db.hibernate.DbSession;
  258. import org.openmrs.api.db.hibernate.DbSessionFactory;
  259. import org.openmrs.module.basicexample.Item;
  260. import org.springframework.beans.factory.annotation.Autowired;
  261. import org.springframework.stereotype.Repository;
  262.  
  263. @Repository("basicexample.BasicexampleDao")
  264. public class BasicexampleDao {
  265.  
  266. @Autowired
  267. DbSessionFactory sessionFactory;
  268.  
  269. private DbSession getSession() {
  270. return sessionFactory.getCurrentSession();
  271. }
  272.  
  273. public Item getItemByUuid(String uuid) {
  274. return (Item) getSession().createCriteria(Item.class).add(Restrictions.eq("uuid", uuid)).uniqueResult();
  275. }
  276.  
  277. public List<Item> getAllItems() {
  278. Criteria crit = sessionFactory.getCurrentSession().createCriteria(Item.class);
  279. return crit.list();
  280. }
  281.  
  282. public Item saveItem(Item item) {
  283. getSession().saveOrUpdate(item);
  284. return item;
  285. }
  286. }
  287.  
  288.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement