Commit 9e9fd2b0 authored by Christof Wolke's avatar Christof Wolke

save Answers

parent 8ccf1040
package classes.apps.feedback;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import java.io.Serializable;
import java.util.List;
import co.uk.rushorm.core.RushObject;
/**
* Created by Christof on 06.09.2016.
*/
@JsonObject
public class Answer extends RushObject implements Serializable {
@JsonField(name="id")
private int answerid;
@JsonField(name="possibleAnswers")
private List<String> possibleAnswers;
public Answer() {
}
public Answer(int answerid, List<String> possibleAnswers) {
this.answerid = answerid;
this.possibleAnswers = possibleAnswers;
}
public int getAnswerid() {
return answerid;
}
public void setAnswerid(int answerid) {
this.answerid = answerid;
}
public List<String> getPossibleAnswers() {
return possibleAnswers;
}
public void setPossibleAnswers(List<String> possibleAnswers) {
this.possibleAnswers = possibleAnswers;
}
@Override
public String toString() {
return "Answer{" +
"answerid=" + answerid +
", possibleAnswers=" + possibleAnswers +
'}';
}
}
......@@ -4,10 +4,10 @@ import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import co.uk.rushorm.core.RushObject;
import co.uk.rushorm.core.annotations.RushList;
/**
* Created by Christof on 06.09.2016.
......@@ -15,9 +15,6 @@ import co.uk.rushorm.core.annotations.RushList;
@JsonObject
public class Question extends RushObject implements Serializable {
@RushList(classType = Answer.class)
@JsonField(name = "answers")
private ArrayList<Answer> answers;
@JsonField(name = "id")
private int questionId;
......@@ -25,14 +22,17 @@ public class Question extends RushObject implements Serializable {
@JsonField(name = "text")
private String text;
@JsonField(name="possibleAnswers")
private List<String> possibleAnswers;
public Question() {
}
public Question(int questionId, String text, ArrayList<Answer> answers) {
public Question(int questionId, String text, List<String> possibleAnswers) {
this.questionId = questionId;
this.text = text;
this.answers = answers;
this.possibleAnswers = possibleAnswers;
}
public int getQuestionId() {
......@@ -51,12 +51,12 @@ public class Question extends RushObject implements Serializable {
this.text = text;
}
public ArrayList<Answer> getAnswers() {
return answers;
public List<String> getPossibleAnswers() {
return possibleAnswers;
}
public void setAnswers(ArrayList<Answer> answers) {
this.answers = answers;
public void setPossibleAnswers(List<String> possibleAnswers) {
this.possibleAnswers = possibleAnswers;
}
@Override
......@@ -64,7 +64,7 @@ public class Question extends RushObject implements Serializable {
return "Question{" +
"questionId=" + questionId +
", text='" + text + '\'' +
", answers=" + answers +
", possibleAnswers=" + possibleAnswers +
'}';
}
}
......@@ -2,26 +2,22 @@ package de.uni_oldenburg.bidl.member_view;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import classes.Event;
import classes.apps.feedback.Answer;
import classes.apps.feedback.Question;
import de.uni_oldenburg.bidl.BaseActivity;
import de.uni_oldenburg.bidl.R;
import services.db.DBHelper;
import services.servercommunication.NetworkService;
......@@ -32,7 +28,9 @@ public class FeedbackActivity extends AppCompatActivity {
private LinearLayout mLinearLayout;
private Button btSendFeedback;
private RadioGroup radioGroup;
private int numberOfButtons = 0;
private List<RadioButton> buttons = new ArrayList<>();
private LinkedHashMap<Integer, String> hashMap = new LinkedHashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -41,77 +39,84 @@ public class FeedbackActivity extends AppCompatActivity {
mLinearLayout = (LinearLayout) findViewById(R.id.feedback_ll);
btSendFeedback = (Button) findViewById(R.id.sendFeedbackBt);
setTitle(getIntent().getStringExtra("appName"));
btSendFeedback.setEnabled(true);
btSendFeedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
radioGroup.getCheckedRadioButtonId();
for (RadioButton radioButton : buttons){
if (radioButton.isChecked()){
hashMap.put((Integer) radioButton.getTag(), (String) radioButton.getText());
}
}
if (hashMap != null && hashMap.size() > 0){
saveCheckedAnswers(hashMap);
}else {
showMessage(R.string.noButtonClicked);
}
}
});
}
@Override
protected void onStart() {
super.onStart();
event = DBHelper.getEvent();
NetworkService.getQuestions(event.getEventId(), new NetworkService.RequestGetQuestionsHandler() {
@Override
public void done(List<Question> questions) {
setQuestions(questions);
getAnswers();
public void done(final List<Question> questions) {
if (questions != null){
setQuestions(questions);
FeedbackActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
for (Question question : questions) {
initGui(question);
}
}
});
} else{
TextView tv = new TextView(getApplicationContext());
tv.setText(R.string.noQuestions);
tv.setGravity(Gravity.CENTER);
tv.setSingleLine(false);
}
}
});
}
@Override
protected void onPause() {
super.onPause();
mLinearLayout.removeAllViews();
mLinearLayout.removeAllViews();
}
private void getAnswers() {
for (final Question question : questions){
NetworkService.getPossibleAnswers(question.getQuestionId(), new NetworkService.RequestGetPossibleAnswersHandler() {
@Override
public void done(final Answer answer) {
if (answer != null){
FeedbackActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
initGui(question, answer);
}
});
}
}
});
}
}
private void initGui(Question question, Answer answer) {
for (int row = 0; row < 1; row++){
private void initGui(Question question) {
for (int row = 0; row < 1; row++) {
TextView tv = new TextView(this);
tv.setText(question.getText());
tv.setGravity(Gravity.CENTER);
tv.setPadding(0,40,0,0);
tv.setPadding(0, 40, 0, 0);
radioGroup = new RadioGroup(this);
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(LinearLayout.HORIZONTAL);
radioGroup.setGravity(Gravity.CENTER);
for (int i = 0; i < answer.getPossibleAnswers().size(); i++){
for (int i = 0; i < question.getPossibleAnswers().size(); i++) {
RadioButton radioButton = new RadioButton(this);
radioButton.setId((row * 2 )+i);
radioButton.setText(answer.getPossibleAnswers().get(i));
radioButton.setId(numberOfButtons);
radioButton.setTag(question.getQuestionId());
radioButton.setText(question.getPossibleAnswers().get(i));
radioGroup.addView(radioButton);
buttons.add(radioButton);
numberOfButtons++;
}
mLinearLayout.addView(tv);
mLinearLayout.addView(radioGroup);
......@@ -119,6 +124,32 @@ public class FeedbackActivity extends AppCompatActivity {
}
private void saveCheckedAnswers(LinkedHashMap<Integer, String> hashMap) {
for (Map.Entry<Integer, String> entry : hashMap.entrySet()){
NetworkService.saveAnswer(entry.getKey(), entry.getValue(), new NetworkService.RequestSaveAnswerHandler() {
@Override
public void done(final String status) {
if (status.equals("ok")){
FeedbackActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
btSendFeedback.setEnabled(false);
}
});
} else {
showMessage(R.string.error_cannot_save_answer);
}
}
});
}
showMessage(R.string.answersSaved);
}
private void showMessage(int message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
......
......@@ -82,7 +82,7 @@ public class ForumActivity extends AppCompatActivity implements ListCommunicator
User user = localStorage.getLoggedInUser();
if(yourMessage.getText() != null && yourMessage.getText().length() > 0) {
NetworkService.setNewComment(yourMessage.getText().toString(), (int) user.getUserId(), forum.getForumId(), new NetworkService.RequestNewCommentHandler() {
NetworkService.setNewComment(yourMessage.getText().toString(), forum.getForumId(), (int) user.getUserId(), new NetworkService.RequestNewCommentHandler() {
@Override
public void done(String status) {
if (status.equals("ok")) {
......
......@@ -3,12 +3,8 @@ package services.db;
import android.app.Application;
import android.content.Context;
import java.util.List;
import classes.Event;
import classes.LocalStorage;
import classes.apps.AppContainer;
import classes.apps.feedback.Answer;
import co.uk.rushorm.core.RushCore;
import co.uk.rushorm.core.RushSearch;
......@@ -55,7 +51,6 @@ public class DBHelper {
*/
public static void clearAll() {
clearEvents();
clearAnswers();
}
......@@ -63,16 +58,5 @@ public class DBHelper {
RushCore.getInstance().deleteAll(Event.class);
}
//Answer
public static List<Answer> getAllAnswer(){
List<Answer> answers = new RushSearch().find(Answer.class);
return answers;
}
public static void clearAnswers(){RushCore.getInstance().deleteAll(Answer.class);}
}
package services.servercommunication;
import android.util.Log;
import com.bluelinelabs.logansquare.LoganSquare;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
......@@ -18,7 +16,6 @@ import java.util.concurrent.TimeUnit;
import classes.Event;
import classes.StatusResponse;
import classes.User;
import classes.apps.feedback.Answer;
import classes.apps.feedback.Question;
import classes.apps.forum.Forum;
......@@ -309,17 +306,16 @@ public abstract class NetworkService {
void done(List<Question> questions);
}
public static void getPossibleAnswers(int questionId, final RequestGetPossibleAnswersHandler handler){
public static void saveAnswer(int questionId, String finalAnswer, final RequestSaveAnswerHandler handler){
okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);
RequestBody requestBody = new FormEncodingBuilder()
.add("questionId", String.valueOf(questionId))
.add("finalAnswer", finalAnswer)
.build();
Request req = new Request.Builder()
.url(REST.SPRING_GETPOSSIBLEANSWERS)
.url(REST.SPRING_SAVEANSWER)
.post(requestBody)
.build();
......@@ -328,26 +324,26 @@ public abstract class NetworkService {
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
handler.done(null);
handler.done("failure");
e.printStackTrace();
}
@Override
public void onResponse(Response response) throws IOException {
try {
if(response.isSuccessful()){
Answer answer = LoganSquare.parse(response.body().byteStream(), Answer.class);
handler.done(answer);
if (response.isSuccessful()){
StatusResponse statusResponse = LoganSquare.parse(response.body().byteStream(), StatusResponse.class);
handler.done(statusResponse.getStatus());
}
} catch (IOException e){
handler.done(null);
handler.done("failure");
e.printStackTrace();
}
}
});
}
public interface RequestGetPossibleAnswersHandler{
void done(Answer answer);
public interface RequestSaveAnswerHandler{
void done (String status);
}
}
......@@ -49,7 +49,8 @@ public abstract class REST {
private static final String GETQUESTIONS = "/getQuestions";
public static String SPRING_GETQUESTIONS = FULL_URL + FEEDBACK + GETQUESTIONS;
private static final String GETPOSSIBLEANSWERS = "/getPossibleAnswers";
public static String SPRING_GETPOSSIBLEANSWERS = FULL_URL + FEEDBACK + GETPOSSIBLEANSWERS;
private static final String SAVEANSWER = "/saveAnswer";
public static String SPRING_SAVEANSWER = FULL_URL + FEEDBACK + SAVEANSWER;
}
......@@ -21,6 +21,10 @@
<!-- FeedbackActivity !-->
<string name="sendFeedbackBtn">Abstimmen!</string>
<string name="answersSaved">Danke für Ihr Feedback!</string>
<string name="noButtonClicked">Es wurde keine Antwort ausgewählt!</string>
<string name="error_cannot_save_answer">Ihre Antwort konnte nicht gespeichert werden!</string>
<string name="noQuestions">Es wurden zu dieser Vorlesung noch keine Fragen bereitgestellt!</string>
<!-- ForumActivity !-->
<string name="sendMessageHint">Ihre Nachricht</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment