Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.68 KB | None | 0 0
  1. package simpleblog.service.endpoint;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. import com.firebase.client.DataSnapshot;
  11. import com.firebase.client.Firebase;
  12. import com.firebase.client.FirebaseError;
  13. import com.firebase.client.ValueEventListener;
  14.  
  15. import java.io.BufferedReader;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.InputStreamReader;
  19. import java.net.MalformedURLException;
  20. import java.net.URL;
  21. import java.util.List;
  22. import java.util.logging.Level;
  23.  
  24. import javax.jws.WebMethod;
  25. import javax.jws.WebService;
  26.  
  27. import org.json.simple.JSONArray;
  28. import org.json.simple.JSONObject;
  29. import org.json.simple.parser.JSONParser;
  30. import org.json.simple.parser.ParseException;
  31.  
  32. @WebService(serviceName = "Simpleblog")
  33. public class Simpleblog {
  34. private Firebase postRef;
  35. private Firebase userRef;
  36. private Firebase commentRef;
  37.  
  38. public Simpleblog() {
  39. postRef = new Firebase("https://simpleblog31.firebaseio.com/Post");
  40. userRef = new Firebase("https://simpleblog31.firebaseio.com/User");
  41. commentRef = new Firebase("https://simpleblog31.firebaseio.com/Comment");
  42. }
  43. @WebMethod(operationName = "addPost")
  44. public boolean addPost(String judul, String konten, String tanggal) {
  45. Firebase newdataRef = postRef.push();
  46.  
  47. Map<String, String> post = new HashMap<String, String>();
  48. post.put("judul", judul);
  49. post.put("konten", konten);
  50. post.put("tanggal", tanggal);
  51. post.put("status", "unpublished");
  52.  
  53. newdataRef.setValue(post);
  54. return true;
  55. }
  56.  
  57. @WebMethod(operationName = "getPost")
  58. public Post getPost(final String id) {
  59. Firebase getRef = postRef.child(id);
  60. final Post post = new Post();
  61.  
  62. // Fetch post from firebase
  63. postRef.addValueEventListener(new ValueEventListener() {
  64.  
  65. @Override
  66. public void onDataChange(DataSnapshot snapshot) {
  67. String judul = snapshot.child(id).child("judul").getValue().toString();
  68. String konten = snapshot.child(id).child("konten").getValue().toString();
  69. String tanggal = snapshot.child(id).child("tanggal").getValue().toString();
  70. String status = snapshot.child(id).child("status").getValue().toString();
  71.  
  72. post.setId(id);
  73. post.setJudul(judul);
  74. post.setKonten(konten);
  75. post.setTanggal(tanggal);
  76. post.setStatus(status);
  77. }
  78.  
  79. @Override
  80. public void onCancelled(FirebaseError error) {
  81. System.out.println("The read failed: " + error.getMessage());
  82. }
  83. });
  84.  
  85. while(post==null) {}
  86. if(post!=null) return post;
  87. else return null;
  88. }
  89.  
  90. @WebMethod(operationName = "listPost")
  91. public Post[] listPost() {
  92. final ArrayList<Post> list = new ArrayList<Post>();
  93.  
  94. // Fetch post from firebase
  95. postRef.addValueEventListener(new ValueEventListener() {
  96.  
  97. @Override
  98. public void onDataChange(DataSnapshot snapshot) {
  99.  
  100. for(DataSnapshot child : snapshot.getChildren()) {
  101. String id = child.getKey();
  102. String judul = child.child("judul").getValue().toString();
  103. String konten = child.child("konten").getValue().toString();
  104. String tanggal = child.child("tanggal").getValue().toString();
  105. String status = child.child("status").getValue().toString();
  106. System.out.println(judul);
  107. Post post = new Post();
  108. post.setId(id);
  109. post.setJudul(judul);
  110. post.setKonten(konten);
  111. post.setTanggal(tanggal);
  112. post.setStatus(status);
  113. list.add(post);
  114. }
  115. }
  116.  
  117. @Override
  118. public void onCancelled(FirebaseError error) {
  119. System.out.println("The read failed: " + error.getMessage());
  120. }
  121. });
  122.  
  123. while(list.size()==0) {
  124. System.out.println(list.size());
  125. }
  126. // Convert ArrayList to Array
  127. Post[] arrayPost = new Post[list.size()];
  128. list.toArray(arrayPost);
  129. return arrayPost;
  130. }
  131.  
  132. @WebMethod(operationName = "editPost")
  133. public boolean editPost(String id, String judul, String konten, String tanggal){
  134. Map<String, Object> post = new HashMap<String, Object>();
  135. post.put("judul", judul);
  136. post.put("konten", konten);
  137. post.put("tanggal", tanggal);
  138. Firebase updateRef = postRef.child(id);
  139.  
  140. updateRef.updateChildren(post);
  141.  
  142. return true;
  143. }
  144.  
  145. public boolean deletePost(String id) {
  146. Firebase delRef = postRef.child(id);
  147. delRef.removeValue();
  148.  
  149. return true;
  150. }
  151.  
  152. public boolean publishPost(String id) {
  153. Map<String, Object> post = new HashMap<String, Object>();
  154. post.put("status", "published");
  155. Firebase publishRef = postRef.child(id);
  156.  
  157. publishRef.updateChildren(post);
  158. return true;
  159. }
  160.  
  161. public boolean addUser(String nama, String password, String email, String role) {
  162. Firebase newUserRef = userRef.push();
  163.  
  164. Map<String, String> user = new HashMap<String, String>();
  165. user.put("nama", nama);
  166. user.put("password", password);
  167. user.put("email", email);
  168. user.put("role", role);
  169.  
  170. newUserRef.setValue(user);
  171. return true;
  172. }
  173.  
  174. public User[] listUser() {
  175. List<User> list = new ArrayList<User>();
  176. JSONObject json = getJSON("https://simpleblog31.firebaseio.com" + "/User.json");
  177.  
  178. Map<String,Object>map = (Map<String,Object>)json;
  179. for(Map.Entry<String,Object> m : map.entrySet()){
  180. {
  181. Map<String,String>user = (Map<String,String>) m.getValue();
  182. String id,nama,email,password,role;
  183. id = m.getKey();
  184. nama = user.get("nama");
  185. email = user.get("email");
  186. password = user.get("password");
  187. role = user.get("role");
  188.  
  189. User temp = new User();
  190. temp.setId(id);
  191. temp.setName(nama);
  192. temp.setEmail(email);
  193. temp.setPassword(password);
  194. temp.setRole(role);
  195.  
  196. list.add(temp);
  197. }
  198. }
  199.  
  200. // Convert ArrayList to Array
  201. User[] arrayUser = new User[list.size()];
  202. list.toArray(arrayUser);
  203.  
  204. return arrayUser;
  205. }
  206.  
  207. public boolean editUser(String id, String nama, String password, String role, String email) {
  208. Map<String, Object> user = new HashMap<String, Object>();
  209. user.put("nama", nama);
  210. user.put("password", password);
  211. user.put("email", email);
  212. user.put("role", role);
  213. Firebase updateRef = userRef.child(id);
  214.  
  215. updateRef.updateChildren(user);
  216.  
  217. return true;
  218. }
  219.  
  220. public boolean deleteUser(String id) {
  221. Firebase delRef = userRef.child(id);
  222. delRef.removeValue();
  223.  
  224. return true;
  225. }
  226.  
  227. public boolean addComment(String postId, String nama, String email, String konten) {
  228. Firebase newCommentRef = commentRef.push();
  229.  
  230. Map<String, String> comment = new HashMap<String, String>();
  231. comment.put("post_id", postId);
  232. comment.put("nama", nama);
  233. comment.put("email", email);
  234. comment.put("konten", konten);
  235.  
  236. String tgl = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  237. comment.put("tanggal", tgl);
  238.  
  239. newCommentRef.setValue(comment);
  240. return true;
  241. }
  242. private JSONObject getJSON(String path)
  243. {
  244. URL url;
  245. InputStream is = null;
  246. BufferedReader br;
  247. String JSON = "";
  248. String line = "";
  249. JSONArray array = new JSONArray();
  250.  
  251. try {
  252. url = new URL(path);
  253. is = url.openStream();
  254. br = new BufferedReader(new InputStreamReader(is));
  255. while((line = br.readLine()) != null)
  256. {
  257. JSON += line;
  258. }
  259. JSONParser parser = new JSONParser();
  260. Object obj = parser.parse(JSON);
  261. array.add(obj);
  262. } catch (MalformedURLException ex) {
  263. System.out.println(ex.toString());
  264. java.util.logging.Logger.getLogger(Simpleblog.class.getName()).log(Level.SEVERE, null, ex);
  265. } catch (IOException ex) {
  266. System.out.println(ex.toString());
  267. java.util.logging.Logger.getLogger(Simpleblog.class.getName()).log(Level.SEVERE, null, ex);
  268. } catch (ParseException ex) {
  269. System.out.println(ex.toString());
  270. java.util.logging.Logger.getLogger(Simpleblog.class.getName()).log(Level.SEVERE, null, ex);
  271. }
  272. JSONObject obj = (JSONObject) array.get(0);
  273. return obj;
  274. }
  275.  
  276. public Comment[] listComment(String postId) {
  277. List<Comment> list = new ArrayList<Comment>();
  278. JSONObject json = getJSON("https://simpleblog31.firebaseio.com" + "/Comment.json");
  279.  
  280. Map<String,Object>map = (Map<String,Object>)json;
  281. for(Map.Entry<String,Object> m : map.entrySet()){
  282. {
  283. Map<String,String>comment = (Map<String,String>) m.getValue();
  284. String id,pId,name,email,konten,tanggal;
  285. id = m.getKey();
  286. email = comment.get("email");
  287. konten = comment.get("konten");
  288. name = comment.get("nama");
  289. pId = comment.get("post_id");
  290. tanggal = comment.get("tanggal");
  291.  
  292. Comment temp = new Comment();
  293. temp.setId(id);
  294. temp.setPostId(postId);
  295. temp.setName(name);
  296. temp.setEmail(email);
  297. temp.setKonten(konten);
  298. temp.setTanggal(tanggal);
  299.  
  300. if(temp.getPostId()==postId)
  301. {
  302. list.add(temp);
  303. }
  304. }
  305. }
  306.  
  307. Comment[] arrayComment = new Comment[list.size()];
  308. list.toArray(arrayComment);
  309.  
  310. return arrayComment;
  311. }
  312.  
  313. public boolean deleteComment(String id) {
  314. Firebase delRef = commentRef.child(id);
  315. delRef.removeValue();
  316.  
  317. return true;
  318. }
  319.  
  320. public Post[] search(String query) {
  321. query = query.toLowerCase();
  322. List<Post> list = new ArrayList<Post>();
  323. JSONObject json = getJSON("https://simpleblog31.firebaseio.com" + "/Post.json");
  324.  
  325. Map<String,Object>map = (Map<String,Object>)json;
  326. for(Map.Entry<String,Object> m : map.entrySet()){
  327. {
  328. Map<String,String>post = (Map<String,String>) m.getValue();
  329. String id,judul,status,konten,tanggal;
  330. id = m.getKey();
  331. judul = post.get("judul");
  332. konten = post.get("konten");
  333. status = post.get("status");
  334. tanggal = post.get("tanggal");
  335.  
  336. if(konten.toLowerCase().contains(query) || judul.contains(query))
  337. {
  338. Post temp = new Post();
  339. temp.setId(id);
  340. temp.setJudul(judul);
  341. temp.setKonten(konten);
  342. temp.setStatus(status);
  343. temp.setTanggal(tanggal);
  344.  
  345. list.add(temp);
  346. }
  347. }
  348. }
  349.  
  350. // Convert ArrayList to Array
  351. Post[] arrayPost = new Post[list.size()];
  352. list.toArray(arrayPost);
  353. return arrayPost;
  354. }
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement