/* * 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.Course; public class CourseManager { private static final String SOAP_ACTION = "http://backoffice.dippler.htk.tlu.ee/courseManager"; private static final String METHOD_NAME = "courseManager"; private static final String NAMESPACE = "http://backoffice.dippler.htk.tlu.ee/"; private Context app_context; private ProfileManager profileManager; public CourseManager(Context context) { this.app_context = context; this.profileManager = new ProfileManager(context); } private Context getLocalContext() { return this.app_context; } public Course loadMetaData(String sessionId, Course course) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getLocalContext()); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("sessId", sessionId); request.addProperty("action", "LOAD"); request.addProperty("data", buildDataXML(course)); 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()); course = getTitle(response.toString(), course); course.setFacilitator(profileManager.loadProfile(sessionId, course.getOwner_id())); return course; } catch (Exception e) { Log.i("dippler-app", "Failed: "+e.getLocalizedMessage()); e.printStackTrace(); } return null; } private String buildDataXML(Course course) { StringBuffer xml = new StringBuffer(""); xml.append(""); xml.append(""+course.getId()+""); xml.append(""); return xml.toString(); } public Course getTitle(String raw_xml, final Course course) { try { final RootElement root = new RootElement("response"); final Element data = root.getChild("data").getChild("course"); data.getChild("title").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { course.setTitle(body); } }); data.getChild("start").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { course.setStart_date(body); } }); data.getChild("owner").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { course.setOwner_id(body); } }); data.getChild("end").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { course.setEnd_date(body); } }); Xml.parse(raw_xml, root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return course; } }