Created
June 14, 2016 18:29
-
-
Save ryanschuhler/bd1afe9f889b8bf0ec3e8c271286139a to your computer and use it in GitHub Desktop.
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
| /** | |
| * Copyright (c) 2000-present Liferay, Inc. All rights reserved. | |
| * | |
| * This library is free software; you can redistribute it and/or modify it under | |
| * the terms of the GNU Lesser General Public License as published by the Free | |
| * Software Foundation; either version 2.1 of the License, or (at your option) | |
| * any later version. | |
| * | |
| * This library is distributed in the hope that it will be useful, but WITHOUT | |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | |
| * details. | |
| */ | |
| package com.liferay.osb.hubspot.datareplication.service.impl; | |
| import com.liferay.counter.service.CounterLocalServiceUtil; | |
| import com.liferay.osb.hubspot.datareplication.model.HSContact; | |
| import com.liferay.osb.hubspot.datareplication.service.HSContactLocalServiceUtil; | |
| import com.liferay.osb.hubspot.datareplication.service.base.HSContactLocalServiceBaseImpl; | |
| import com.liferay.osb.hubspot.datareplication.service.persistence.HSContactUtil; | |
| import com.liferay.portal.kernel.exception.PortalException; | |
| import com.liferay.portal.kernel.exception.SystemException; | |
| import com.liferay.portal.kernel.json.JSONArray; | |
| import com.liferay.portal.kernel.json.JSONFactoryUtil; | |
| import com.liferay.portal.kernel.json.JSONObject; | |
| import com.liferay.portal.kernel.util.DateUtil; | |
| import com.liferay.portal.kernel.util.GetterUtil; | |
| import com.liferay.portal.kernel.util.Validator; | |
| import com.liferay.portlet.expando.model.ExpandoBridge; | |
| import com.liferay.portlet.expando.model.ExpandoColumn; | |
| import com.liferay.portlet.expando.model.ExpandoColumnConstants; | |
| import com.liferay.portlet.expando.model.ExpandoTable; | |
| import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil; | |
| import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil; | |
| import java.util.Iterator; | |
| /** | |
| * @author Ryan Schuhler | |
| */ | |
| public class HSContactLocalServiceImpl extends HSContactLocalServiceBaseImpl { | |
| public void checkHSData() throws SystemException { | |
| JSONObject hubspotJSON = fetchRecentContacts(); | |
| processData(hubspotJSON); | |
| } | |
| public JSONObject fetchRecentContacts() throws SystemException { | |
| JSONObject hubspotJSON = JSONFactoryUtil.createJSONObject(); | |
| return hubspotJSON; | |
| } | |
| public void processData(JSONObject hubspotJSON) throws SystemException { | |
| JSONArray contactsJSON = hubspotJSON.getJSONArray("contacts"); | |
| for (int i = 0; i < contactsJSON.length(); i++) { | |
| System.out.println("===================="); | |
| JSONObject contactJSON = contactsJSON.getJSONObject(i); | |
| updateContact(contactJSON); | |
| } | |
| } | |
| public void updateContact(JSONObject contactJSON) throws SystemException { | |
| JSONObject properties = contactJSON.getJSONObject("properties"); | |
| if (Validator.isNull(properties)) { | |
| return; | |
| } | |
| String email = getPropertyValue(properties, "email"); | |
| String firstName = getPropertyValue(properties, "firstname"); | |
| String addedAt = contactJSON.getString("addedAt"); | |
| long hsCreateDate = GetterUtil.getLong(addedAt); | |
| String lastModifiedDate = getPropertyValue(properties, "lastmodifieddate"); | |
| long hsModifiedDate = GetterUtil.getLong(lastModifiedDate); | |
| String lastName = getPropertyValue(properties, "lastname"); | |
| System.out.println(email); | |
| //Fetch HSContact | |
| HSContact hsContact = HSContactUtil.fetchByEmail(email); | |
| System.out.println(hsContact); | |
| //Create HSContact if doesnt exist | |
| if (Validator.isNull(hsContact)) { | |
| long hsContactId = CounterLocalServiceUtil.increment( | |
| HSContact.class.getName()); | |
| HSContact newHSContact = HSContactLocalServiceUtil.createHSContact( | |
| hsContactId); | |
| newHSContact.setCreateDate(DateUtil.newDate()); | |
| newHSContact.setModifiedDate(DateUtil.newDate()); | |
| newHSContact.setEmail(email); | |
| newHSContact.setFirstName(firstName); | |
| newHSContact.setHsCreateDate(DateUtil.newDate(hsCreateDate)); | |
| newHSContact.setHsModifiedDate(DateUtil.newDate(hsModifiedDate)); | |
| newHSContact.setLastName(lastName); | |
| HSContactLocalServiceUtil.addHSContact(newHSContact); | |
| } | |
| else { | |
| hsContact.setEmail(email); | |
| hsContact.setFirstName(firstName); | |
| hsContact.setHsModifiedDate(DateUtil.newDate(hsModifiedDate)); | |
| hsContact.setLastName(lastName); | |
| hsContact.setModifiedDate(DateUtil.newDate()); | |
| } | |
| //Request JSON by contact Email | |
| //updateContactProperties(hsContact, contactJSON); | |
| } | |
| public void updateContactProperties(String email, JSONObject contactJSON) | |
| throws SystemException { | |
| HSContact hsContact = HSContactUtil.fetchByEmail(email); | |
| System.out.println(hsContact); | |
| ExpandoBridge expandoBridge = hsContact.getExpandoBridge(); | |
| System.out.println("expandoBridgeAttributes"); | |
| System.out.println(expandoBridge.getAttributes()); | |
| //Set contact properties | |
| JSONObject properties = contactJSON.getJSONObject("properties"); | |
| if (Validator.isNull(properties)) { | |
| return; | |
| } | |
| Iterator<String> iterator = properties.keys(); | |
| while (iterator.hasNext()) { | |
| System.out.println("------------------"); | |
| String key = iterator.next(); | |
| JSONObject object = properties.getJSONObject(key); | |
| String value = object.getString("value"); | |
| JSONArray versions = object.getJSONArray("versions"); | |
| JSONObject latest = versions.getJSONObject(0); | |
| String timestamp = latest.getString("timestamp"); | |
| System.out.println(key); | |
| System.out.println(value); | |
| System.out.println(timestamp); | |
| //Update Contact expandos | |
| checkExpandoColumn(key); | |
| expandoBridge.setAttribute(key, value, false); | |
| } | |
| //Set contact form submissions | |
| JSONArray formSubmissions = contactJSON.getJSONArray( | |
| "form-submissions"); | |
| if (Validator.isNull(properties)) { | |
| return; | |
| } | |
| for (int i = 0; i < formSubmissions.length(); i++) { | |
| System.out.println("------------------"); | |
| JSONObject formSubmission = formSubmissions.getJSONObject(i); | |
| System.out.println("Form Submission " + i); | |
| System.out.println(formSubmission); | |
| //Update Contact expandos | |
| } | |
| } | |
| private void checkExpandoColumn(String columnName) | |
| throws SystemException { | |
| long companyId = GetterUtil.getLong(20155); | |
| ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.fetchDefaultTable( | |
| companyId, HSContact.class.getName()); | |
| if (Validator.isNull(expandoTable)) { | |
| expandoTable = ExpandoTableLocalServiceUtil.addTable( | |
| companyId, HSContact.class.getName(), "HubSpot"); | |
| } | |
| ExpandoColumn expandoColumn = null; | |
| try { | |
| expandoColumn = ExpandoColumnLocalServiceUtil.getColumn( | |
| expandoTable.getTableId(), columnName); | |
| } | |
| catch (PortalException e) { | |
| expandoColumn = ExpandoColumnLocalServiceUtil.addColumn( | |
| expandoTable.getTableId(), columnName, | |
| ExpandoColumnConstants.STRING); | |
| } | |
| } | |
| private String getPropertyValue(JSONObject jsonObject, String name) { | |
| JSONObject json = jsonObject.getJSONObject(name); | |
| return json.getString("value"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment