Advertisement
Guest User

Untitled

a guest
Oct 6th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. package org.openmrs.module.basicexample.web.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.openmrs.module.webservices.rest.web.RestConstants;
  6. import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController;
  7.  
  8. /**
  9. * @author levine
  10. */
  11. @Controller
  12. @RequestMapping("/rest/" + RestConstants.VERSION_1 + "/basicexample")
  13. public class BasicexampleResourceController extends MainResourceController {
  14.  
  15. /**
  16. * @see org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController#getNamespace()
  17. */
  18. @Override
  19. public String getNamespace() {
  20. return "v1/basicexample";
  21. }
  22. }
  23.  
  24.  
  25.  
  26.  
  27. //==============================================================================================================================
  28.  
  29.  
  30. package org.openmrs.module.basicexample.web.controller;
  31.  
  32. import java.util.List;
  33. import org.openmrs.api.context.Context;
  34. import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource;
  35. import org.openmrs.module.basicexample.Item;
  36. import org.openmrs.module.basicexample.api.BasicexampleService;
  37. import org.openmrs.module.webservices.rest.web.RequestContext;
  38. import org.openmrs.module.webservices.rest.web.RestConstants;
  39. import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
  40. import org.openmrs.module.webservices.rest.web.annotation.Resource;
  41. import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
  42. import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
  43. import org.openmrs.module.webservices.rest.web.representation.Representation;
  44. import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
  45. import org.openmrs.module.webservices.rest.web.response.ResponseException;
  46. import org.openmrs.module.webservices.rest.web.resource.impl.DataDelegatingCrudResource;
  47. import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
  48. import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
  49.  
  50. /**
  51. * @author levine
  52. */
  53. @Resource(name = RestConstants.VERSION_1 + "/item", supportedClass = Item.class, supportedOpenmrsVersions = { "2.0.*",
  54. "2.1.*", "2.2.*", "2.3.*", "2.4.* " })
  55. public class ItemResource extends DataDelegatingCrudResource<Item> {
  56.  
  57. @Override
  58. public Item getByUniqueId(String string) {
  59. System.out.println("****************GET: ");
  60. List<Item> its = Context.getService(BasicexampleService.class).getAllItems();
  61.  
  62. Item it = new Item();
  63. it.setDescription("my description");
  64. return it;
  65. }
  66.  
  67. public NeedsPaging<Item> doGetAll(RequestContext context) {
  68. return new NeedsPaging<Item>(Context.getService(BasicexampleService.class).getAllItems(), context);
  69. }
  70.  
  71. @Override
  72. protected void delete(Item t, String string, RequestContext rc) throws ResponseException {
  73. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  74. }
  75.  
  76. @Override
  77. public void purge(Item t, RequestContext rc) throws ResponseException {
  78. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  79. }
  80.  
  81. @Override
  82. public DelegatingResourceDescription getRepresentationDescription(Representation r) {
  83. if (r instanceof DefaultRepresentation) {
  84. DelegatingResourceDescription description = new DelegatingResourceDescription();
  85. description.addProperty("uuid");
  86. description.addProperty("description");
  87. description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
  88. description.addSelfLink();
  89. return description;
  90. } else if (r instanceof FullRepresentation) {
  91. DelegatingResourceDescription description = new DelegatingResourceDescription();
  92. description.addProperty("uuid");
  93. description.addProperty("description");
  94. description.addSelfLink();
  95. return description;
  96. }
  97. return null;
  98. }
  99.  
  100. @Override
  101. public Item newDelegate() {
  102. return new Item();
  103. }
  104.  
  105. @Override
  106. public Item save(Item t) {
  107. Item item = Context.getService(BasicexampleService.class).saveItem(t);
  108. return item;
  109. }
  110.  
  111. @PropertyGetter("display")
  112. public String getDisplayString(Item item) {
  113. return item.getDescription();
  114. }
  115.  
  116. }
  117.  
  118.  
  119.  
  120. //=============================================================================================================================
  121.  
  122.  
  123. package org.openmrs.module.basicexample;
  124.  
  125. import org.openmrs.BaseOpenmrsData;
  126. import org.openmrs.User;
  127.  
  128. import javax.persistence.Basic;
  129. import javax.persistence.Column;
  130. import javax.persistence.Entity;
  131. import javax.persistence.GeneratedValue;
  132. import javax.persistence.Id;
  133. import javax.persistence.JoinColumn;
  134. import javax.persistence.ManyToOne;
  135. import javax.persistence.Table;
  136.  
  137. @Entity(name = "basicexample.Item")
  138. @Table(name = "basicexample_item")
  139. public class Item extends BaseOpenmrsData {
  140.  
  141. @Id
  142. @GeneratedValue
  143. @Column(name = "basicexample_item_id")
  144. private Integer id;
  145.  
  146. @ManyToOne
  147. @JoinColumn(name = "owner")
  148. private User owner;
  149.  
  150. @Basic
  151. @Column(name = "description", length = 255)
  152. private String description;
  153.  
  154. @Override
  155. public Integer getId() {
  156. return id;
  157. }
  158.  
  159. @Override
  160. public void setId(Integer id) {
  161. this.id = id;
  162. }
  163.  
  164. @Override
  165. public String getUuid() {
  166. return super.getUuid();
  167. }
  168.  
  169. @Override
  170. public void setUuid(String uuid) {
  171. super.setUuid(uuid);
  172. }
  173.  
  174. public User getOwner() {
  175. return owner;
  176. }
  177.  
  178. public void setOwner(User owner) {
  179. this.owner = owner;
  180. }
  181.  
  182. public String getDescription() {
  183. return description;
  184. }
  185.  
  186. public void setDescription(String description) {
  187. this.description = description;
  188. }
  189. }
  190.  
  191.  
  192.  
  193. //========================================================================================================================
  194.  
  195.  
  196. package org.openmrs.module.basicexample.api.impl;
  197.  
  198. import java.util.List;
  199. import org.openmrs.api.APIException;
  200. import org.openmrs.api.UserService;
  201. import org.openmrs.api.impl.BaseOpenmrsService;
  202. import org.openmrs.module.basicexample.Item;
  203. import org.openmrs.module.basicexample.api.BasicexampleService;
  204. import org.openmrs.module.basicexample.api.dao.BasicexampleDao;
  205.  
  206. public class BasicexampleServiceImpl extends BaseOpenmrsService implements BasicexampleService {
  207.  
  208. BasicexampleDao dao;
  209.  
  210. UserService userService;
  211.  
  212. /**
  213. * Injected in moduleApplicationContext.xml
  214. */
  215. public void setDao(BasicexampleDao dao) {
  216. this.dao = dao;
  217. }
  218.  
  219. /**
  220. * Injected in moduleApplicationContext.xml
  221. */
  222. public void setUserService(UserService userService) {
  223. this.userService = userService;
  224. }
  225.  
  226. @Override
  227. public Item getItemByUuid(String uuid) throws APIException {
  228. return dao.getItemByUuid(uuid);
  229. }
  230.  
  231. @Override
  232. public List<Item> getAllItems() throws APIException {
  233. return dao.getAllItems();
  234. }
  235.  
  236. @Override
  237. public Item saveItem(Item item) throws APIException {
  238. if (item.getOwner() == null) {
  239. item.setOwner(userService.getUser(1));
  240. }
  241.  
  242. return dao.saveItem(item);
  243. }
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement