/* * 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 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.EndTextElementListener; import android.sax.RootElement; import android.util.Log; import android.util.Xml; import ee.htk.dippler.app.entities.Blogpost; public class BlogManager { private static final String SOAP_ACTION = "http://backoffice.dippler.htk.tlu.ee/blogManager"; private static final String METHOD_NAME = "blogManager"; private static final String NAMESPACE = "http://backoffice.dippler.htk.tlu.ee/"; private Context app_context; public BlogManager(Context context) { this.app_context = context; } private Context getLocalContext() { return this.app_context; } public Blogpost getBlogpost(String sessionId, int post_id) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getLocalContext()); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("sessId", sessionId); request.addProperty("action", "LOAD_POST"); request.addProperty("data", buildDataXML(post_id)); 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", "Course xml " + response.toString()); return parsePost(response.toString()); } catch (Exception e) { Log.i("dippler-app", "Failed: "+e.getLocalizedMessage()); e.printStackTrace(); } return null; } private String buildDataXML(int post_id) { StringBuffer xml = new StringBuffer(""); xml.append(""); xml.append(""+post_id+""); xml.append(""); return xml.toString(); } public Blogpost parsePost(String raw_xml) { final Blogpost blogpost = new Blogpost(); try { final RootElement root = new RootElement("response"); final Element data = root.getChild("data").getChild("courseblog_post"); data.getChild("title").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { blogpost.setTitle(body); } }); data.getChild("body").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { blogpost.setBody(body); } }); data.getChild("created").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { blogpost.setPosted_on(body); } }); Xml.parse(raw_xml, root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return blogpost; } }