If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.example.jens.myapplication.apimanager.manager;
//fromwww.java2s.comimport com.example.jens.myapplication.apimanager.ApiConnection;
import com.example.jens.myapplication.apimanager.CancellableTask;
import com.example.jens.myapplication.apimanager.PostRequestTask;
import com.example.jens.myapplication.apimanager.RequestParams;
import com.example.jens.myapplication.apimanager.SimpleRequestTask;
import com.example.jens.myapplication.apimanager.filter.PersonFilter;
import com.example.jens.myapplication.domain.Person;
import com.example.jens.myapplication.sam.JoetzApplication;
import com.example.jens.myapplication.util.JsonUtils;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/**
* Manager to manage Api calls for bookings
*/publicclass BookingManager {
//Fill in %d with the camp ID
privatestaticfinal String URL = "api/camps/%d/register";
privatestaticfinal String FILTER_CONTACT = "contactFilter";
privatestaticfinal String FILTER_BOND = "bondFilter";
privatestaticfinal String FILTER_ANON = "anonFilter";
/**
* Book an amount of people for a certain camp
* @param campId The camp ID you wish to register to
* @param contactPerson The contactperson for the booking
* @param personIds The person IDs for the booking (for MyPersons or user's person object)
* @param anonPersons Anonymous person objects
* @param socialSecurityPersons Persons with social security info [optional]
* @param afterTask Task to be executed when the request is finished
* @return the task
*/publicstatic CancellableTask bookPeople(
long campId,
Person contactPerson,
List<Long> personIds,
List<Person> anonPersons,
Person[] socialSecurityPersons,
final SimpleRequestTask afterTask){
if(!JoetzApplication.getContext().getLoginManager().isLoggedIn()){
thrownew IllegalStateException("Required to be logged in");
}
String url = String.format(URL, campId);
BookingModel obj = new BookingModel();
obj.contactPerson = contactPerson;
obj.personIds = personIds;
List<RegisterAnonModel> listAnons = new LinkedList<RegisterAnonModel>();
for(Person p : anonPersons){
RegisterAnonModel ram = new RegisterAnonModel();
ram.addToUserAccount = false;
ram.person = p;
listAnons.add(ram);
}
obj.anonPersons = listAnons;
switch(socialSecurityPersons.length){
case 2: obj.secondParentSocialSecurity = socialSecurityPersons[1];
case 1: obj.firstParentSocialSecurity = socialSecurityPersons[0];
}
FilterProvider filter = JsonUtils.createSimpleFilter(new JsonUtils.Filters()
.addFilter(FILTER_CONTACT, PersonFilter.CONTACT_PERSON.fields())
.addFilter(FILTER_ANON, PersonFilter.ANONYMOUS_PARTICIPANT.fields())
.addFilter(FILTER_BOND, PersonFilter.SOCIAL_SECURITY.fields()));
String rawBody = null;
try {
rawBody = new ObjectMapper().writer(filter).writeValueAsString(obj);
} catch (IOException e) {
e.printStackTrace();
}
RequestParams rp = new RequestParams();
rp.setRawBody(rawBody);
rp.setAuthKey(JoetzApplication.getContext().getLoginManager().getAuthKey());
return ApiConnection.post(url,
JsonUtils.createSimpleUserTask(),
JsonUtils.createSimpleRefreshUserTask(afterTask), rp);
}
/**
* Model for booking (for json conversion)
*/privatestaticclass BookingModel {
@JsonFilter(FILTER_CONTACT)
@JsonProperty("ContactPerson")
Person contactPerson;
@JsonProperty("PersonIds")
List<Long> personIds;
@JsonProperty("AnonymousPersons")
List<RegisterAnonModel> anonPersons;
@JsonFilter(FILTER_BOND)
@JsonProperty("FirstParentSocialSecurityInformation")
Person firstParentSocialSecurity;
@JsonFilter(FILTER_BOND)
@JsonProperty("SecondParentSocialSecurityInformation")
Person secondParentSocialSecurity;
}
/**
* Model for anonymous person (for json conversion)
*/privatestaticclass RegisterAnonModel{
@JsonProperty("AddToUserAccount")
boolean addToUserAccount;
@JsonFilter(FILTER_ANON)
@JsonProperty("Information")
Person person;
}
}