Skip to content

Commit

Permalink
API return value, and redirect call
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-stastny authored and jirikrepl committed Apr 21, 2016
1 parent 7a4de30 commit e135adc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 23 deletions.
87 changes: 67 additions & 20 deletions client/src/main/java/cz/incad/kramerius/client/tools/Share.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import static cz.incad.kramerius.client.utils.ApiCallsHelp.getJSON;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.configuration.ConfigurationException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cz.incad.kramerius.client.utils.ApiCallsHelp;
import cz.incad.kramerius.utils.ApplicationURL;
import cz.incad.kramerius.utils.StringUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;
Expand All @@ -34,17 +33,24 @@ public void configure(Map props) {
String pid = req.getParameter("pid");
this.applicationUrl = ApplicationURL.applicationURL(req);
if (pid != null && StringUtils.isAnyString(pid)) {
String api = KConfiguration.getInstance().getConfiguration().getString("api.point");
if (!api.endsWith("/")) {
api += "/";
}
String jsoned = getJSON(api+"item/"+pid+"");
this.itemObject = new JSONObject(jsoned);
JSONObject rJsonObject = jsonFromAPI(pid);
this.itemObject = rJsonObject;
}
} catch (JSONException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
}


protected JSONObject jsonFromAPI(String pid) {
String api = KConfiguration.getInstance().getConfiguration().getString("api.point");
if (!api.endsWith("/")) {
api += "/";
}
String jsoned = getJSON(api+"item/"+pid+"");
JSONObject rJsonObject = new JSONObject(jsoned);
return rJsonObject;
}


public String getRootTitle() {
Expand All @@ -61,13 +67,63 @@ public String getRootTitle() {
public String getTitle() {
try {
if (this.itemObject != null) {
return this.itemObject.getString("title");
List<String> titles = new ArrayList<String>();
JSONArray jsonArray = selectContext(this.itemObject.getJSONArray("context"));
if (jsonArray != null) {
for (int i = 0,ll=jsonArray.length(); i < ll; i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
String pid = jsonObj.getString("pid");
titles.add(normalizedTitleFromGivenJSON(pid));
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < titles.size(); i++) {
if (i > 0) builder.append(" > ");
builder.append(titles.get(i));
}
return builder.toString();
} else {
return this.itemObject.getString("title");
}
} else return "";
} catch (JSONException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
return "";
}
}

private JSONArray selectContext(JSONArray jsonArray) {
if (jsonArray.length() > 0) return jsonArray.getJSONArray(0);
else return null;
}


private String normalizedTitleFromGivenJSON(String pid) {
JSONObject jsonFromAPI = jsonFromAPI(pid);
if (jsonFromAPI.getString("model").equals("periodicalitem")) {
JSONObject jsonObject = jsonFromAPI.getJSONObject("details");
if (jsonObject.has("date")) {
return jsonObject.getString("date");
}
if (jsonObject.has("issueNumber")) {
return jsonObject.getString("issueNumber");
}
if (jsonObject.has("partNumber")) {
return jsonObject.getString("partNumber");
}
return jsonFromAPI.getString("title");
} else if (jsonFromAPI.getString("model").equals("periodicalvolume")) {
JSONObject jsonObject = jsonFromAPI.getJSONObject("details");
if (jsonObject.has("year")) {
return jsonObject.getString("year");
}
return jsonFromAPI.getString("title");
} else {
String title = jsonFromAPI.getString("title");
if (StringUtils.isAnyString(title)) {
return title.length() > 20 ? title.substring(0, 20)+ " ... " : title;
} else return "";
}
}

public String getModel() {
try {
Expand Down Expand Up @@ -127,13 +183,4 @@ public String getDetails() {
return "";
}
}
/*
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "uuid%253A5035a48a-5e2e-486c-8127-2fa650842e46";
String string = URLDecoder.decode(str,"UTF-8");
System.out.println(string);
String nstring = URLDecoder.decode(string,"UTF-8");
System.out.println(nstring);
}*/

}
4 changes: 2 additions & 2 deletions client/src/main/webapp/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ ClientAPIDev.prototype = {
var href = "";
if (withParams) {
$('#search_form input[name="page"]').val("doc")
href += "?" + $("#search_form").serialize() + "#" + pid;
href += "index.vm?" + $("#search_form").serialize() + "#" + pid;
} else {
href += "page=doc#" + pid;
href += "index.vm?page=doc#" + pid;
}
window.location.assign(href);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ public Response postProfile(JSONObject rawdata) {
UserProfile profile = this.userProfileManager.getProfile(user);
profile.setJSONData(rawdata);
this.userProfileManager.saveProfile(user, profile);
return Response.ok().entity(profile).build();
return Response.ok().entity(profile.getJSONData().toString()).build();
}
}

0 comments on commit e135adc

Please sign in to comment.