/* * 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.Profile; public class ProfileManager { private static final String SOAP_ACTION = "http://backoffice.dippler.htk.tlu.ee/profileManager"; private static final String METHOD_NAME = "profileManager"; private static final String NAMESPACE = "http://backoffice.dippler.htk.tlu.ee/"; private Context app_context; public ProfileManager(Context context) { this.app_context = context; } private Context getLocalContext() { return this.app_context; } public Profile loadProfile(String sessionId, int id) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getLocalContext()); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("sessId", sessionId); request.addProperty("action", "LOAD"); request.addProperty("data", buildDataXML(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", "profile xml " + response.toString()); return this.parseProfile(response.toString()); } catch (Exception e) { Log.i("dippler-app", "Failed: "+e.getLocalizedMessage()); e.printStackTrace(); } return null; } private String buildDataXML(int id) { StringBuffer xml = new StringBuffer(""); xml.append(""); xml.append(""+id+""); xml.append(""); return xml.toString(); } private Profile parseProfile(String raw_xml) { final Profile profile = new Profile(); try { final RootElement root = new RootElement("response"); final Element data = root.getChild("data").getChild("profile"); data.getChild("firstname").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { profile.setFirstname(body); } }); data.getChild("lastname").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { profile.setLastname(body); } }); data.getChild("email").setEndTextElementListener( new EndTextElementListener() { public void end(String body) { profile.setEmail(body); } }); Xml.parse(raw_xml, root.getContentHandler()); } catch (Exception e) { e.printStackTrace(); } return profile; } }