Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package simpleblog.service.endpoint;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import com.firebase.client.DataSnapshot;
- import com.firebase.client.Firebase;
- import com.firebase.client.FirebaseError;
- import com.firebase.client.ValueEventListener;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.List;
- import java.util.logging.Level;
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- @WebService(serviceName = "Simpleblog")
- public class Simpleblog {
- private Firebase postRef;
- private Firebase userRef;
- private Firebase commentRef;
- public Simpleblog() {
- postRef = new Firebase("https://simpleblog31.firebaseio.com/Post");
- userRef = new Firebase("https://simpleblog31.firebaseio.com/User");
- commentRef = new Firebase("https://simpleblog31.firebaseio.com/Comment");
- }
- @WebMethod(operationName = "addPost")
- public boolean addPost(String judul, String konten, String tanggal) {
- Firebase newdataRef = postRef.push();
- Map<String, String> post = new HashMap<String, String>();
- post.put("judul", judul);
- post.put("konten", konten);
- post.put("tanggal", tanggal);
- post.put("status", "unpublished");
- newdataRef.setValue(post);
- return true;
- }
- @WebMethod(operationName = "getPost")
- public Post getPost(final String id) {
- Firebase getRef = postRef.child(id);
- final Post post = new Post();
- // Fetch post from firebase
- postRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot snapshot) {
- String judul = snapshot.child(id).child("judul").getValue().toString();
- String konten = snapshot.child(id).child("konten").getValue().toString();
- String tanggal = snapshot.child(id).child("tanggal").getValue().toString();
- String status = snapshot.child(id).child("status").getValue().toString();
- post.setId(id);
- post.setJudul(judul);
- post.setKonten(konten);
- post.setTanggal(tanggal);
- post.setStatus(status);
- }
- @Override
- public void onCancelled(FirebaseError error) {
- System.out.println("The read failed: " + error.getMessage());
- }
- });
- while(post==null) {}
- if(post!=null) return post;
- else return null;
- }
- @WebMethod(operationName = "listPost")
- public Post[] listPost() {
- final ArrayList<Post> list = new ArrayList<Post>();
- // Fetch post from firebase
- postRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot snapshot) {
- for(DataSnapshot child : snapshot.getChildren()) {
- String id = child.getKey();
- String judul = child.child("judul").getValue().toString();
- String konten = child.child("konten").getValue().toString();
- String tanggal = child.child("tanggal").getValue().toString();
- String status = child.child("status").getValue().toString();
- System.out.println(judul);
- Post post = new Post();
- post.setId(id);
- post.setJudul(judul);
- post.setKonten(konten);
- post.setTanggal(tanggal);
- post.setStatus(status);
- list.add(post);
- }
- }
- @Override
- public void onCancelled(FirebaseError error) {
- System.out.println("The read failed: " + error.getMessage());
- }
- });
- while(list.size()==0) {
- System.out.println(list.size());
- }
- // Convert ArrayList to Array
- Post[] arrayPost = new Post[list.size()];
- list.toArray(arrayPost);
- return arrayPost;
- }
- @WebMethod(operationName = "editPost")
- public boolean editPost(String id, String judul, String konten, String tanggal){
- Map<String, Object> post = new HashMap<String, Object>();
- post.put("judul", judul);
- post.put("konten", konten);
- post.put("tanggal", tanggal);
- Firebase updateRef = postRef.child(id);
- updateRef.updateChildren(post);
- return true;
- }
- public boolean deletePost(String id) {
- Firebase delRef = postRef.child(id);
- delRef.removeValue();
- return true;
- }
- public boolean publishPost(String id) {
- Map<String, Object> post = new HashMap<String, Object>();
- post.put("status", "published");
- Firebase publishRef = postRef.child(id);
- publishRef.updateChildren(post);
- return true;
- }
- public boolean addUser(String nama, String password, String email, String role) {
- Firebase newUserRef = userRef.push();
- Map<String, String> user = new HashMap<String, String>();
- user.put("nama", nama);
- user.put("password", password);
- user.put("email", email);
- user.put("role", role);
- newUserRef.setValue(user);
- return true;
- }
- public User[] listUser() {
- List<User> list = new ArrayList<User>();
- JSONObject json = getJSON("https://simpleblog31.firebaseio.com" + "/User.json");
- Map<String,Object>map = (Map<String,Object>)json;
- for(Map.Entry<String,Object> m : map.entrySet()){
- {
- Map<String,String>user = (Map<String,String>) m.getValue();
- String id,nama,email,password,role;
- id = m.getKey();
- nama = user.get("nama");
- email = user.get("email");
- password = user.get("password");
- role = user.get("role");
- User temp = new User();
- temp.setId(id);
- temp.setName(nama);
- temp.setEmail(email);
- temp.setPassword(password);
- temp.setRole(role);
- list.add(temp);
- }
- }
- // Convert ArrayList to Array
- User[] arrayUser = new User[list.size()];
- list.toArray(arrayUser);
- return arrayUser;
- }
- public boolean editUser(String id, String nama, String password, String role, String email) {
- Map<String, Object> user = new HashMap<String, Object>();
- user.put("nama", nama);
- user.put("password", password);
- user.put("email", email);
- user.put("role", role);
- Firebase updateRef = userRef.child(id);
- updateRef.updateChildren(user);
- return true;
- }
- public boolean deleteUser(String id) {
- Firebase delRef = userRef.child(id);
- delRef.removeValue();
- return true;
- }
- public boolean addComment(String postId, String nama, String email, String konten) {
- Firebase newCommentRef = commentRef.push();
- Map<String, String> comment = new HashMap<String, String>();
- comment.put("post_id", postId);
- comment.put("nama", nama);
- comment.put("email", email);
- comment.put("konten", konten);
- String tgl = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- comment.put("tanggal", tgl);
- newCommentRef.setValue(comment);
- return true;
- }
- private JSONObject getJSON(String path)
- {
- URL url;
- InputStream is = null;
- BufferedReader br;
- String JSON = "";
- String line = "";
- JSONArray array = new JSONArray();
- try {
- url = new URL(path);
- is = url.openStream();
- br = new BufferedReader(new InputStreamReader(is));
- while((line = br.readLine()) != null)
- {
- JSON += line;
- }
- JSONParser parser = new JSONParser();
- Object obj = parser.parse(JSON);
- array.add(obj);
- } catch (MalformedURLException ex) {
- System.out.println(ex.toString());
- java.util.logging.Logger.getLogger(Simpleblog.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- System.out.println(ex.toString());
- java.util.logging.Logger.getLogger(Simpleblog.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ParseException ex) {
- System.out.println(ex.toString());
- java.util.logging.Logger.getLogger(Simpleblog.class.getName()).log(Level.SEVERE, null, ex);
- }
- JSONObject obj = (JSONObject) array.get(0);
- return obj;
- }
- public Comment[] listComment(String postId) {
- List<Comment> list = new ArrayList<Comment>();
- JSONObject json = getJSON("https://simpleblog31.firebaseio.com" + "/Comment.json");
- Map<String,Object>map = (Map<String,Object>)json;
- for(Map.Entry<String,Object> m : map.entrySet()){
- {
- Map<String,String>comment = (Map<String,String>) m.getValue();
- String id,pId,name,email,konten,tanggal;
- id = m.getKey();
- email = comment.get("email");
- konten = comment.get("konten");
- name = comment.get("nama");
- pId = comment.get("post_id");
- tanggal = comment.get("tanggal");
- Comment temp = new Comment();
- temp.setId(id);
- temp.setPostId(postId);
- temp.setName(name);
- temp.setEmail(email);
- temp.setKonten(konten);
- temp.setTanggal(tanggal);
- if(temp.getPostId()==postId)
- {
- list.add(temp);
- }
- }
- }
- Comment[] arrayComment = new Comment[list.size()];
- list.toArray(arrayComment);
- return arrayComment;
- }
- public boolean deleteComment(String id) {
- Firebase delRef = commentRef.child(id);
- delRef.removeValue();
- return true;
- }
- public Post[] search(String query) {
- query = query.toLowerCase();
- List<Post> list = new ArrayList<Post>();
- JSONObject json = getJSON("https://simpleblog31.firebaseio.com" + "/Post.json");
- Map<String,Object>map = (Map<String,Object>)json;
- for(Map.Entry<String,Object> m : map.entrySet()){
- {
- Map<String,String>post = (Map<String,String>) m.getValue();
- String id,judul,status,konten,tanggal;
- id = m.getKey();
- judul = post.get("judul");
- konten = post.get("konten");
- status = post.get("status");
- tanggal = post.get("tanggal");
- if(konten.toLowerCase().contains(query) || judul.contains(query))
- {
- Post temp = new Post();
- temp.setId(id);
- temp.setJudul(judul);
- temp.setKonten(konten);
- temp.setStatus(status);
- temp.setTanggal(tanggal);
- list.add(temp);
- }
- }
- }
- // Convert ArrayList to Array
- Post[] arrayPost = new Post[list.size()];
- list.toArray(arrayPost);
- return arrayPost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement