/* * Copyright 2012 Tallinn University Centre for Educational Technology * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ee.htk.dippler.app.service; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.sax.Element; import android.sax.EndElementListener; import android.sax.EndTextElementListener; import android.sax.RootElement; import android.util.Log; import android.util.Xml; import ee.htk.dippler.app.entities.Answer; import ee.htk.dippler.app.entities.Assignment; import ee.htk.dippler.app.entities.Blogpost; import ee.htk.dippler.app.entities.Comment; import ee.htk.dippler.app.entities.Course; import ee.htk.dippler.app.entities.Event; import ee.htk.dippler.utils.ServiceDate; public class ListingManager { private Context app_context = null; private String sessionId = null; private static final String SOAP_ACTION = "http://backoffice.dippler.htk.tlu.ee/listingManager"; private static final String METHOD_NAME = "listingManager"; private static final String NAMESPACE = "http://backoffice.dippler.htk.tlu.ee/"; private static String dc_uri = "http://purl.org/dc/elements/1.1/"; private static String content_uri = "http://purl.org/rss/1.0/modules/content/"; private int request_status = 0; //default 0 private String message = ""; public ListingManager(Context context, String sessionId) { this.app_context = context; this.sessionId = sessionId; } public ListingManager() { } private Context getLocalContext() { return this.app_context; } private String getSessionId() { return this.sessionId; } private void setStatus(String status) { this.request_status = Integer.parseInt(status); } public int getStatus() { return this.request_status; } private void setMessage(String msg) { this.message = msg; } public String getMessage() { return this.message; } private String query(String parent, String object, int offset, int limit) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getLocalContext()); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("sessId", getSessionId()); request.addProperty("parent", parent); request.addProperty("object", object); request.addProperty("offset", offset); request.addProperty("limit", limit); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); //If preference not set, fallback to default URL HttpTransportSE ht = new HttpTransportSE(prefs.getString("dippler-bos-wsdl", "http://htk.tlu.ee/backoffice/BackOfficeService?wsdl")); Log.i("dippler-app", "Starting"); try { ht.call(SOAP_ACTION, envelope); SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); Log.i("dippler-app", "QUERY OBJECT " + response.toString()); return response.toString(); } catch (Exception e) { Log.i("dippler-app", "Failed: "+e.getLocalizedMessage()); e.printStackTrace(); } return null; } public List getCourses() { final ArrayList courses = new ArrayList(); final CourseManager courseManager = new CourseManager(getLocalContext()); try { final RootElement root = new RootElement("response"); final Course tmp_course = new Course(); final Element data = root.getChild("data"); root.getChild("status").setEndTextElementListener(new EndTextElementListener() { @Override public void end(String body) { setStatus(body); } }); root.getChild("message").setEndTextElementListener(new EndTextElementListener() { @Override public void end(String body) { setMessage(body); } }); data.getChild("id").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { try { int course_id = Integer.parseInt(body); tmp_course.setId(course_id); } catch(NumberFormatException e) { Log.d("dippler-app", "Course ID parse failed: "+body); } //Just in case check for debugging if ( tmp_course.getId() > 0 ) { Course course_with_metadata = courseManager.loadMetaData(getSessionId(), tmp_course.getInstance()); if ( course_with_metadata != null ) { course_with_metadata.setStart(ServiceDate.format(getLocalContext(), course_with_metadata.getStart_date())); course_with_metadata.setEnd(ServiceDate.format(getLocalContext(), course_with_metadata.getEnd_date())); courses.add(course_with_metadata); } } } }); Xml.parse(query("", "COURSE_ENROLLED", 0, 0), root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return courses; } public List getNews(int course_id) { final ArrayList blogPosts = new ArrayList(); final BlogManager blogManager = new BlogManager(getLocalContext()); try { final RootElement root = new RootElement("response"); final Element data = root.getChild("data"); data.getChild("id").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { try { int post_id = Integer.parseInt(body); Blogpost post = blogManager.getBlogpost(getSessionId(), post_id); if ( post != null ) { post.setPosted_on(ServiceDate.format(getLocalContext(), post.getPosted_on())); blogPosts.add(post); } } catch(NumberFormatException e) { Log.d("dippler-app", "Blog ID parse failed: "+body); } } }); Xml.parse(query(course_id+"", "BLOGPOST", 0, 0 ) , root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return blogPosts; } public List getEvents(int course_id, int offset, int limit) { final ArrayList events = new ArrayList(); try { final RootElement root = new RootElement("response"); final Event tmp_event = new Event(); final Element data = root.getChild("data").getChild("activities").getChild("activity"); data.setEndElementListener(new EndElementListener() { @Override public void end() { events.add(tmp_event.getInstance()); } }); data.getChild("id").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setId(body); } }); data.getChild("event").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setEvent(body); } }); data.getChild("object_type").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setObject_type(body); } }); data.getChild("object_title").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setObject_title(body); } }); data.getChild("creator_fullname").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setCreator(body); } }); data.getChild("created").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setDate(ServiceDate.format(getLocalContext(), body)); } }); Xml.parse(query(course_id+"", "ACTIVITY", offset, limit ) , root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return events; } public List getAssignments(int course_id) { final ArrayList tasks = new ArrayList(); try { final RootElement root = new RootElement("response"); final Assignment tmp_event = new Assignment(); final Element data = root.getChild("data").getChild("assignments").getChild("assignment"); data.setEndElementListener(new EndElementListener() { @Override public void end() { tasks.add(tmp_event.getInstance()); } }); data.getChild("title").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setTitle(body); } }); data.getChild("deadline").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setDeadline(ServiceDate.format(getLocalContext(), body)); } }); data.getChild("maxpoints").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setPoints(body); } }); data.getChild("subtype").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setType(body); } }); data.getChild("late_submission").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setLate_submission(body); } }); data.getChild("submission_visibility").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setVisibility(body); } }); data.getChild("description").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_event.setContent(body); } }); Xml.parse(query(course_id+"", "ASSIGNMENT_FULL", 0, 0 ) , root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return tasks; } public List getAnswers(int course_id) { final ArrayList tasks = new ArrayList(); try { final RootElement root = new RootElement("response"); final Answer tmp_answer = new Answer(); final Element data = root.getChild("data").getChild("answers").getChild("answer"); data.setEndElementListener(new EndElementListener() { @Override public void end() { tasks.add(tmp_answer.getInstance()); } }); data.getChild("assignment_title").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setTitle(body); } }); data.getChild("grade").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setGrade(body); } }); data.getChild("maxpoints").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setMaxpoints(body); } }); data.getChild("feedback").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setFeedback(body); } }); Xml.parse(query(course_id+"", "USER_COURSE_ANSWERS_MOBILE", 0, 0 ) , root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return tasks; } public List getComments(String blog_url) { final ArrayList tasks = new ArrayList(); try { final RootElement root = new RootElement("rss"); final Comment tmp_answer = new Comment(); final Element data = root.getChild("channel").getChild("item"); data.setEndElementListener(new EndElementListener() { @Override public void end() { tasks.add(tmp_answer.getInstance()); } }); data.getChild("title").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setPostTitle(body); } }); data.getChild("link").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setUrl(body); } }); data.getChild(dc_uri, "creator").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setCreator(body); } }); data.getChild("pubDate").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setCreatedDate(ServiceDate.format(getLocalContext(), body, ServiceDate.DATE_FORMAT_WP)); } }); data.getChild(content_uri, "encoded").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { tmp_answer.setBody(body); } }); //Todo use some query param checks Xml.parse(this.httpGET(blog_url+"?feed=comments-rss2") , root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return tasks; } private String httpGET(String url) { String response = null; try { HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is // established. int timeoutConnection = 15000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 15000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(httpParameters); HttpGet httpget = new HttpGet(new URI(url)); ; // Execute HTTP Get Request ResponseHandler responseHandler = new BasicResponseHandler(); // Cache response response = httpclient.execute(httpget, responseHandler); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } return response; } }