android-openrat

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 739f5ffcd339218fe858df9b3d05ed67a5c0be4e
parent 4e2d304748f97cad509ddc435ac7c1465fd4a6a4
Author: dankert <devnull@localhost>
Date:   Wed, 19 Oct 2011 00:57:33 +0200

Anlegen von neuen Ordnern und Seiten.

Diffstat:
res/layout/new1.xml | 11++++++-----
res/values/strings.xml | 1+
src/de/openrat/android/blog/FolderActivity.java | 10++++++----
src/de/openrat/android/blog/NewActivity.java | 84++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
src/de/openrat/android/blog/OpenRatBlog.java | 2+-
src/de/openrat/android/blog/util/OpenRatClientAsyncTask.java | 6++++--
src/de/openrat/client/OpenRatClient.java | 111++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
7 files changed, 182 insertions(+), 43 deletions(-)

diff --git a/res/layout/new1.xml b/res/layout/new1.xml @@ -9,15 +9,16 @@ <TableRow> <TextView android:text="@string/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp"></TextView> - <EditText android:text="@+id/EditText01" android:id="@+id/EditText01" + <EditText android:id="@+id/newname" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> - </TableRow> + </TableRow> <TableRow android:id="@+id/template"> <TextView android:text="@string/template" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> - <RadioGroup android:id="@+id/RadioGroupTemplates" - android:layout_width="wrap_content" android:layout_height="wrap_content" - android:orientation="vertical"></RadioGroup> + <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" + android:layout_height="wrap_content" android:drawSelectorOnTop="true" + android:prompt="@string/template" /> + </TableRow> </TableLayout> diff --git a/res/values/strings.xml b/res/values/strings.xml @@ -4,6 +4,7 @@ <string name="app_name">OpenRat</string> <string name="preferences">Einstellungen</string> <string name="username">Benutzername</string> + <string name="path">Pfad</string> <string name="hostname">Hostname</string> <string name="password">Kennwort</string> diff --git a/src/de/openrat/android/blog/FolderActivity.java b/src/de/openrat/android/blog/FolderActivity.java @@ -104,9 +104,11 @@ public class FolderActivity extends ListActivity { // folderid = getIntent().getStringExtra("folderid"); - try { + if ( folderid == null ) + folderid = client.getRootFolder(); + data = client.getFolderEntries(folderid); } catch (IOException e) @@ -383,9 +385,9 @@ public class FolderActivity extends ListActivity case R.id.menu_newpage: intent = new Intent(this, NewActivity.class); - intent.putExtra("request", getIntent().getSerializableExtra( - "request")); - intent.putExtra("menuid", item.getItemId()); + intent.putExtra(NewActivity.EXTRA_CLIENT, client); + intent.putExtra(NewActivity.EXTRA_MENUID, item.getItemId()); + intent.putExtra(NewActivity.EXTRA_FOLDERID, folderid); startActivity(intent); return true; diff --git a/src/de/openrat/android/blog/NewActivity.java b/src/de/openrat/android/blog/NewActivity.java @@ -1,19 +1,32 @@ package de.openrat.android.blog; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; +import android.widget.ArrayAdapter; import android.widget.Button; -import android.widget.RadioButton; -import android.widget.RadioGroup; +import android.widget.EditText; +import android.widget.Spinner; +import de.openrat.android.blog.util.OpenRatClientAsyncTask; import de.openrat.client.OpenRatClient; public class NewActivity extends Activity { + public static final String EXTRA_CLIENT = "request"; + public static final String EXTRA_MENUID = "menuid"; + public static final String EXTRA_FOLDERID = "folderid"; + private OpenRatClient request; private int menuid; + private Map<String, String> templates; + private String folderid; /** * {@inheritDoc} @@ -24,24 +37,44 @@ public class NewActivity extends Activity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - request = (OpenRatClient) getIntent().getSerializableExtra("request"); - menuid = getIntent().getIntExtra("menuid", 0); + request = (OpenRatClient) getIntent() + .getSerializableExtra(EXTRA_CLIENT); + menuid = getIntent().getIntExtra(EXTRA_MENUID, 0); + folderid = getIntent().getStringExtra(EXTRA_FOLDERID); setContentView(R.layout.new1); - final RadioGroup radioGroupTemplates = (RadioGroup) findViewById(R.id.RadioGroupTemplates); + final EditText editText = (EditText) findViewById(R.id.newname); + final Spinner spinner = (Spinner) findViewById(R.id.spinner); if (menuid == R.id.menu_newpage) { - radioGroupTemplates.setVisibility(View.VISIBLE); + spinner.setVisibility(View.VISIBLE); + + new OpenRatClientAsyncTask(this, R.string.waitingforcontent) + { - RadioButton radioButton = new RadioButton(this); - radioButton.setText("test"); - radioGroupTemplates.addView(radioButton); + @Override + protected void callServer() throws IOException + { + templates = request.getTemplates(); + } + @Override + protected void doOnSuccess() + { + final List<String> valueList = new ArrayList<String>( + templates.values()); + ArrayAdapter adapter = new ArrayAdapter(NewActivity.this, + android.R.layout.simple_spinner_item, valueList); + adapter + .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + } + }.execute(); } else { - radioGroupTemplates.setVisibility(View.INVISIBLE); + spinner.setVisibility(View.INVISIBLE); } final Button button = (Button) findViewById(R.id.button_save); @@ -50,7 +83,36 @@ public class NewActivity extends Activity @Override public void onClick(View v) { - + + new OpenRatClientAsyncTask(NewActivity.this, + R.string.waitingforcontent) + { + + @Override + protected void callServer() throws IOException + { + if (menuid == R.id.menu_newfolder) + { + request.createFolder(folderid, editText.getText() + .toString()); + } + if (menuid == R.id.menu_newpage) + { + + int pos = spinner.getSelectedItemPosition(); + final String templateid = NewActivity.this.templates + .keySet().toArray(new String[] {})[pos]; + request.createPage(folderid, editText.getText() + .toString(), templateid); + } + } + + @Override + protected void doOnSuccess() + { + NewActivity.this.finish(); + } + }.execute(); } }); } diff --git a/src/de/openrat/android/blog/OpenRatBlog.java b/src/de/openrat/android/blog/OpenRatBlog.java @@ -72,7 +72,7 @@ public class OpenRatBlog extends Activity @Override public void onClick(View v) { - new OpenRatClientAsyncTask(OpenRatBlog.this, R.string.loading, + new OpenRatClientAsyncTask(OpenRatBlog.this, R.string.waitingforlogin) { @Override diff --git a/src/de/openrat/android/blog/util/OpenRatClientAsyncTask.java b/src/de/openrat/android/blog/util/OpenRatClientAsyncTask.java @@ -5,6 +5,8 @@ package de.openrat.android.blog.util; import java.io.IOException; +import de.openrat.android.blog.R; + import android.app.AlertDialog; import android.app.ProgressDialog; import android.app.AlertDialog.Builder; @@ -24,12 +26,12 @@ public abstract class OpenRatClientAsyncTask extends private AlertDialog alertDialog; private IOException error; - public OpenRatClientAsyncTask(Context context, int title, int message) + public OpenRatClientAsyncTask(Context context, int message) { this.context = context; this.progressDialog = new ProgressDialog(context); - progressDialog.setTitle(title); + progressDialog.setTitle(R.string.loading); progressDialog.setMessage(context.getResources().getString(message)); } diff --git a/src/de/openrat/client/OpenRatClient.java b/src/de/openrat/client/OpenRatClient.java @@ -5,6 +5,8 @@ import java.io.IOException; import java.net.SocketTimeoutException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -69,30 +71,38 @@ public class OpenRatClient extends CMSRequest * Rootfolder geladen. * @return Ordner-Einträge */ - public List<FolderEntry> getFolderEntries(String folderid) - throws IOException + public String getRootFolder() throws IOException { - if (folderid == null) - { + clearParameters(); + setAction("tree"); + setActionMethod("load"); - clearParameters(); - setAction("tree"); - setActionMethod("load"); - - JSONObject json = readJSON(); + JSONObject json = readJSON(); - try - { - folderid = json.getJSONArray("zeilen").getJSONObject(1) - .getString("name"); - } - catch (JSONException e) - { - throw new OpenRatClientException( - "JSON-Error while resolving root folder", e); - } + try + { + String folderid = json.getJSONArray("zeilen").getJSONObject(1) + .getString("name"); + return folderid; } + catch (JSONException e) + { + throw new OpenRatClientException( + "JSON-Error while resolving root folder", e); + } + } + /** + * Ermittelt den Inhalt eines Ordners. + * + * @param folderid + * Id des zu ladenen Ordners. Falls <code>null</code>, wird der + * Rootfolder geladen. + * @return Ordner-Einträge + */ + public List<FolderEntry> getFolderEntries(String folderid) + throws IOException + { final List<FolderEntry> data = new ArrayList<FolderEntry>(); super.clearParameters(); @@ -104,6 +114,9 @@ public class OpenRatClient extends CMSRequest try { JSONObject json = readJSON(); + if (!(json.get("object") instanceof JSONObject)) + return data; // Ordner ist leer. + JSONObject inhalte = json.getJSONObject("object"); JSONArray names = inhalte.names(); @@ -416,7 +429,7 @@ public class OpenRatClient extends CMSRequest { super.clearParameters(); super.setAction(type); - if ( type.equals("page") ) + if (type.equals("page")) super.setActionMethod("prop"); else super.setActionMethod("saveprop"); @@ -430,4 +443,62 @@ public class OpenRatClient extends CMSRequest readJSON(); } + + public Map<String, String> getTemplates() throws IOException + { + super.clearParameters(); + super.setAction("template"); + super.setActionMethod("listing"); + super.setMethod("POST"); + + JSONObject json = readJSON(); + + final Map<String, String> templateMap = new LinkedHashMap<String, String>(); + + try + { + JSONObject templates = json.getJSONObject("templates"); + + for (Iterator ti = templates.keys(); ti.hasNext();) + { + String templateId = (String) ti.next(); + String templateName = templates.getJSONObject(templateId) + .getString("name"); + templateMap.put(templateId, templateName); + } + } + catch (JSONException e) + { + Log.w(this.getClass().getSimpleName(), "\n\n" + json); + throw new OpenRatClientException("a property was not found", e); + } + + return templateMap; + } + + public void createFolder(String folderid, String string) throws IOException + { + super.clearParameters(); + super.setId(folderid); + super.setAction("folder"); + super.setActionMethod("createnewfolder"); + super.setMethod("POST"); + super.setParameter("name", string); + + readJSON(); + } + + public void createPage(String folderid, String string, String templateid) + throws IOException + { + super.clearParameters(); + super.setId(folderid); + super.setAction("folder"); + super.setActionMethod("createnewpage"); + super.setMethod("POST"); + super.setParameter("name", string); + super.setParameter("templateid", templateid); + + readJSON(); + } }