Last active
October 14, 2015 00:28
-
-
Save jwebgordon/4279704 to your computer and use it in GitHub Desktop.
dropdownBean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package beans; | |
import java.io.*; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.TreeMap; | |
import javax.faces.bean.ManagedBean; | |
import javax.faces.bean.ApplicationScoped; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import org.json.JSONTokener; | |
import sun.net.www.http.PosterOutputStream; | |
/** | |
* | |
* @author wgordon | |
*/ | |
@ManagedBean | |
@ApplicationScoped | |
public class BakedBean { | |
String rawLabels; | |
ArrayList labels; | |
String rawValues; | |
ArrayList values; | |
String propName; | |
String propLabel; | |
String groupName; | |
String portalId; | |
String hapikey; | |
TreeMap<String, String> existingProps; | |
HashMap<String, String> propOptions; | |
String selectedPropOption; | |
String existingJson; | |
boolean updateButtonDisabled; | |
/** | |
* Creates a new instance of BakedBean | |
*/ | |
public BakedBean() { | |
labels = new ArrayList(); | |
values = new ArrayList(); | |
propOptions = new HashMap<String, String>(); | |
propOptions.put("Radio", "radio"); | |
propOptions.put("Dropdown", "select"); | |
propOptions.put("Checkboxes", "checkbox"); | |
existingProps = new TreeMap<String, String>(); | |
updateButtonDisabled=true; | |
} | |
public boolean isUpdateButtonDisabled() { | |
return updateButtonDisabled; | |
} | |
public void setUpdateButtonDisabled(boolean updateButtonDisabled) { | |
this.updateButtonDisabled = updateButtonDisabled; | |
} | |
public String getExistingJson() { | |
return existingJson; | |
} | |
public void setExistingJson(String existingJson) { | |
this.existingJson = existingJson; | |
} | |
public HashMap<String, String> getPropOptions() { | |
return propOptions; | |
} | |
public void setPropOptions(HashMap<String, String> propOptions) { | |
this.propOptions = propOptions; | |
} | |
public String getSelectedPropOption() { | |
return selectedPropOption; | |
} | |
public void setSelectedPropOption(String selectedPropOption) { | |
this.selectedPropOption = selectedPropOption; | |
} | |
public TreeMap<String, String> getExistingProps() { | |
return existingProps; | |
} | |
public void setExistingProps(TreeMap<String, String> existingProps) { | |
this.existingProps = existingProps; | |
} | |
public String getHapikey() { | |
return hapikey; | |
} | |
public void setHapikey(String hapikey) { | |
this.hapikey = hapikey; | |
} | |
public String getGroupName() { | |
return groupName; | |
} | |
public void setGroupName(String groupName) { | |
this.groupName = groupName; | |
} | |
public ArrayList getLabels() { | |
return labels; | |
} | |
public void setLabels(ArrayList labels) { | |
this.labels = labels; | |
} | |
public String getPortalId() { | |
return portalId; | |
} | |
public void setPortalId(String portalId) { | |
this.portalId = portalId; | |
} | |
public String getPropLabel() { | |
return propLabel; | |
} | |
public void setPropLabel(String propLabel) { | |
this.propLabel = propLabel; | |
} | |
public String getPropName() { | |
return propName; | |
} | |
public void setPropName(String propName) { | |
this.propName = propName; | |
} | |
public String getRawLabels() { | |
return rawLabels; | |
} | |
public void setRawLabels(String rawLabels) { | |
this.rawLabels = rawLabels; | |
} | |
public String getRawValues() { | |
return rawValues; | |
} | |
public void setRawValues(String rawValues) { | |
this.rawValues = rawValues; | |
} | |
public ArrayList getValues() { | |
return values; | |
} | |
public void setValues(ArrayList values) { | |
this.values = values; | |
} | |
public void printRawLabels() { | |
System.out.println(rawLabels); | |
processRawString(rawLabels); | |
} | |
public ArrayList<String> processRawString(String rawData) { | |
//rawData = rawData.replace("\n", "").replace("\r", ",").replace(" ,", ","); | |
ArrayList<String> destination = new ArrayList<String>(); | |
for (int i = 0; i < rawData.split("\r\n").length; i++) { | |
destination.add(rawData.split("\r\n")[i]); | |
} | |
System.out.println(rawData); | |
return destination; | |
} | |
public void getCurrentProps() { | |
try { | |
URL getPropURL = new URL("https://api.hubapi.com/contacts/v1/properties?portalId=" + portalId + "&hapikey=" + hapikey); | |
HttpURLConnection conn = (HttpURLConnection) getPropURL.openConnection(); | |
conn.setRequestMethod("GET"); | |
conn.setDoOutput(true); | |
conn.setUseCaches(false); | |
conn.connect(); | |
InputStream response = conn.getInputStream(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(response)); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
sb.append(line); | |
} | |
JSONTokener jtoke = new JSONTokener(sb.toString()); | |
JSONArray propJSON = new JSONArray(jtoke); | |
for (int i = 0; i < propJSON.length(); i++) { | |
existingProps.put(propJSON.getJSONObject(i).getString("name"), propJSON.getJSONObject(i).getString("name")); | |
} | |
System.out.println(existingProps.toString()); | |
} catch (Exception e) { | |
System.out.println(e.toString()); | |
} | |
} | |
public void getExistingProp() { | |
try { | |
URL url = new URL("https://api.hubapi.com/contacts/v1/properties/" + propName + "?portalId=" + portalId + "&hapikey=" + hapikey); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
conn.setDoOutput(true); | |
conn.setUseCaches(false); | |
conn.connect(); | |
InputStream response = conn.getInputStream(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(response)); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
sb.append(line); | |
} | |
response.close(); | |
conn.disconnect(); | |
JSONTokener jtoke = new JSONTokener(sb.toString()); | |
JSONObject propJSON = new JSONObject(jtoke); | |
existingJson = propJSON.toString(); | |
updateButtonDisabled=false; | |
} catch (Exception e) { | |
System.out.println(e.toString()); | |
} | |
} | |
public void updateProp() { | |
try { | |
labels = processRawString(rawLabels); | |
values = processRawString(rawValues); | |
TreeMap magicMap = new TreeMap(); | |
if (labels.size() | |
== values.size()) { | |
for (int i = 0; i < labels.size(); i++) { | |
magicMap.put(labels.get(i), values.get(i)); | |
} | |
} else { | |
System.err.println("Labels not same size as values"); | |
return; | |
} | |
JSONArray options = new JSONArray(); | |
for (int i = 0; | |
i < magicMap.size(); | |
i++) { | |
String label = (String) magicMap.keySet().toArray(new Object[magicMap.size()])[i]; | |
String value = (String) magicMap.get(label); | |
try { | |
JSONObject option = new JSONObject(); | |
option.put("label", label); | |
option.put("value", value); | |
option.put("displayOrder", i); | |
options.put(option); | |
} catch (Exception e) { | |
System.out.println(e.toString()); | |
} | |
} | |
JSONObject newJSON = new JSONObject(existingJson); | |
newJSON.put( | |
"options", options); | |
newJSON.put("type","enumeration"); | |
newJSON.put("fieldType", selectedPropOption); | |
System.out.println(newJSON.toString()); | |
URL url2 = new URL("https://api.hubapi.com/contacts/v1/properties/" + propName + "?portalId=" + portalId + "&hapikey=" + hapikey); | |
System.out.println(url2.toString()); | |
HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection(); | |
// conn2.connect(); | |
conn2.setRequestMethod("POST"); | |
conn2.setRequestProperty("Content-Type", "application/json"); | |
conn2.setRequestProperty("X-Override-Internal-Permissions", "true"); | |
conn2.setRequestProperty("Content-Length", Integer.toString(newJSON.toString().length())); | |
conn2.setDoInput(true); | |
conn2.setDoOutput(true); | |
conn2.setUseCaches(false); | |
DataOutputStream osw = new DataOutputStream(conn2.getOutputStream()); | |
System.err.println("JSON POSTING: " + newJSON.toString()); | |
osw.writeBytes(newJSON.toString()); | |
osw.flush(); | |
osw.close(); | |
System.out.println(conn2.getResponseCode()); | |
updateButtonDisabled=true; | |
} catch (Exception e) { | |
System.out.println(e.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment