Created
February 11, 2016 13:04
-
-
Save ramrrr/225e64c27e5552fe1f7c to your computer and use it in GitHub Desktop.
NPE fix
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
| package heimdall.ui.ember.smartphone.app; | |
| import static heimdall.ui.ember.core.EmberSerializer.getBoolean; | |
| import static heimdall.ui.ember.core.EmberSerializer.getLong; | |
| import static heimdall.ui.ember.core.EmberSerializer.getString; | |
| import com.google.gson.Gson; | |
| import com.google.gson.JsonArray; | |
| import com.google.gson.JsonElement; | |
| import com.google.gson.JsonObject; | |
| import com.google.gson.JsonPrimitive; | |
| import heimdall.channel.smartphone.app.SmartphoneAppAttachment; | |
| import heimdall.channel.smartphone.app.SmartphoneAppMessage; | |
| import heimdall.ui.ember.admin.OrganizationScopedSerializer; | |
| import heimdall.ui.ember.core.EmbeddedModelHandler; | |
| import heimdall.ui.ember.core.ModelHandler; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.UUID; | |
| import org.apache.commons.lang3.math.NumberUtils; | |
| import org.joda.time.DateTime; | |
| import org.springframework.util.CollectionUtils; | |
| import com.google.common.collect.Multimap; | |
| import edu.umd.cs.findbugs.annotations.NonNull; | |
| /** | |
| * @author aashish | |
| */ | |
| public class SmartphoneAppMessageModelHandler extends EmbeddedModelHandler<SmartphoneAppMessage> { | |
| private final OrganizationScopedSerializer organizationScopedSerializer; | |
| /** | |
| * Initializing constructor. | |
| * | |
| * @param organizationScopedSerializer serializer for organization scoped entities. | |
| */ | |
| public SmartphoneAppMessageModelHandler(final OrganizationScopedSerializer organizationScopedSerializer) { | |
| super(SmartphoneAppMessage.class); | |
| this.organizationScopedSerializer = organizationScopedSerializer; | |
| } | |
| @Override | |
| public SmartphoneAppMessage newInstance(final String type) { | |
| return new SmartphoneAppMessage(); | |
| } | |
| @Override | |
| public void writeToEntity(final SmartphoneAppMessage entity, final JsonObject data) { | |
| entity.setMessage(getString(data.get("message"))); | |
| entity.setHeadline(getString(data.get("headline"))); | |
| entity.setIsLocationBased(getBoolean(data.get("isLocationBased"))); | |
| entity.setIsAddressBased(getBoolean(data.get("isAddressBased"))); | |
| entity.setResponseType(getString(data.get("responseType"))); | |
| final JsonElement responseAlternatives = data.get("responseAlternatives"); | |
| if (responseAlternatives != null) { | |
| entity.setResponseOptions(responseAlternatives.toString()); | |
| entity.setResponseExpiry(getLong(data.get("responseExpiry"), -1)); | |
| } | |
| if (data.get("attachedFiles") != null) { | |
| List<SmartphoneAppAttachment> attachFiles = new ArrayList<>(); | |
| for (JsonElement jsonElement : data.get("attachedFiles").getAsJsonArray()) { | |
| SmartphoneAppAttachment attachFile = new SmartphoneAppAttachment(); | |
| final JsonObject attachJsonEntry = jsonElement.getAsJsonObject(); | |
| attachFile.setAttachmentFileName(getString(attachJsonEntry.get("fileName"))); | |
| attachFile.setAttachmentKey(getString(attachJsonEntry.get("key"))); | |
| attachFiles.add(attachFile); | |
| } | |
| entity.setAttachmentFiles(attachFiles); | |
| } | |
| DateTime expiryTime = null; | |
| if (data.get("extendedHrs") instanceof JsonPrimitive | |
| && NumberUtils.isDigits(data.get("extendedHrs").getAsString())) { | |
| entity.setExtendedHrs(data.get("extendedHrs").getAsInt()); | |
| expiryTime = new DateTime(); | |
| expiryTime = expiryTime.plusHours(data.get("extendedHrs").getAsInt()); | |
| } | |
| entity.setExpiryDate(expiryTime); | |
| } | |
| @Override | |
| public void writeToJsonObject(@NonNull final SmartphoneAppMessage entity, @NonNull final JsonObject target) { | |
| organizationScopedSerializer.writeToJsonObject(entity, target); | |
| target.addProperty("message", entity.getMessage()); | |
| target.addProperty("headline", entity.getHeadline()); | |
| target.addProperty("isLocationBased", entity.isLocationBased()); | |
| target.addProperty("isAddressBased", entity.isAddressBased()); | |
| target.addProperty("responseType", entity.getResponseType()); | |
| target.addProperty("responseExpiry", entity.getResponseExpiry()); | |
| final String responseOptions = entity.getResponseOptions(); | |
| Gson gson = new Gson(); | |
| final String[] jsonElements = gson.fromJson(responseOptions, String[].class); | |
| final JsonArray responseAlternative = new JsonArray(); | |
| if(jsonElements!=null && jsonElements.length > 0) { | |
| for (String s:jsonElements) { | |
| final JsonPrimitive primitive = new JsonPrimitive(s); | |
| responseAlternative.add(primitive); | |
| } | |
| } | |
| target.add("responseAlternatives", responseAlternative); | |
| final JsonArray jsArr = new JsonArray(); | |
| if (!CollectionUtils.isEmpty(entity.getAttachmentFiles())) { | |
| for (SmartphoneAppAttachment attachFile : entity.getAttachmentFiles()) { | |
| final JsonObject jsObject = new JsonObject(); | |
| jsObject.addProperty("id", UUID.randomUUID().toString()); | |
| jsObject.addProperty("fileName", attachFile.getAttachmentFileName()); | |
| jsObject.addProperty("key", attachFile.getAttachmentKey()); | |
| jsArr.add(jsObject); | |
| } | |
| } | |
| target.add("attachedFiles", jsArr); | |
| if (entity.getExtendedHrs() > 0) { | |
| target.addProperty("extendedHrs", entity.getExtendedHrs()); | |
| } | |
| } | |
| @Override | |
| public void addRelated(@NonNull final SmartphoneAppMessage item, | |
| @NonNull final Multimap<ModelHandler, String> related) { | |
| organizationScopedSerializer.addRelated(item, related); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment