Skip to content

Instantly share code, notes, and snippets.

@fastcodecoq
Created December 6, 2016 19:37
Show Gist options
  • Save fastcodecoq/7d315a1c5680a994a9940e239b9d818f to your computer and use it in GitHub Desktop.
Save fastcodecoq/7d315a1c5680a994a9940e239b9d818f to your computer and use it in GitHub Desktop.
package com.eyenation.model.core.rest;
import com.eyenation.model.core.broker.TagBroker;
import com.eyenation.model.core.broker.VideoBroker;
import com.eyenation.model.core.broker.VideoSoldHistoryBroker;
import com.eyenation.model.core.broker.VideoTagBroker;
import com.eyenation.model.core.exceptions.NonexistentEntityException;
import com.eyenation.model.core.exceptions.PreexistingEntityException;
import com.eyenation.model.core.jpa.CategoryTranslationJpaController;
import com.eyenation.model.core.jpa.TagTranslationJpaController;
import com.eyenation.model.core.jpa.VideoJpaController;
import com.eyenation.model.core.serializers.CategoryTranslationSerializer;
import com.eyenation.model.core.serializers.TagSerializer;
import com.eyenation.model.core.serializers.TagTranslationSerializer;
import com.eyenation.model.core.serializers.VideoSerializer;
import com.eyenation.model.core.serializers.VideoTagSerializer;
import com.eyenation.model.core.video.CategoryTranslation;
import com.eyenation.model.core.video.Tag;
import com.eyenation.model.core.video.TagTranslation;
import com.eyenation.model.core.video.Video;
import com.eyenation.model.core.video.VideoSoldHistory;
import com.eyenation.model.core.video.VideoStatus;
import com.eyenation.model.core.video.VideoTag;
import com.google.gson.Gson;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
@GET
@Path("/fromNumber/{phone}")
public Response getFromNumber(@PathParam("phone") String phone) {
checkNotNull(phone);
System.out.println("PHONE: "+phone);
System.out.println("HERE");
try {
System.out.println("HERE");
VideoBroker broker = VideoBroker.getInstance(EMF);
System.out.println("HEREEE");
Object[] video = broker.videoFromNumber(phone);
for(Object a : video){
System.out.println(a);
}
JSONObject JSon = new JSONObject();
JSon.put("creation_time", video[0]);
JSon.put("unique_name", video[1]);
JSon.put("name", video[2]);
Gson gson = new Gson();
String json = gson.toJson(JSon);
return Response.ok().entity( json ).build();
} catch(NoResultException nex) {
// System.out.println(nex);
return Response.ok().build();
} catch(NullPointerException e) {
// System.out.println(e);
return Response.status(Response.Status.NOT_FOUND).build();
}
}
public Object[] videoFromNumber(String phone){
EntityManager em = emf.createEntityManager();
try{
System.out.println("videoFromNumber Phone: " + phone);
Query q = em.createNativeQuery("SELECT v.video_creation_time, v.video_unique_name, v.video_name, v.person_id as vperson_id FROM video v JOIN person p ON p.home_phone_number = :phone ORDER BY v.video_creation_time DESC LIMIT 1");
q.setParameter("phone", phone);
Object video = q.getSingleResult();
System.out.println(video);
return (Object[]) video;
}catch(Exception e)
{
System.out.println("ERROR VideoB: " + e.getMessage());
return null;
} finally {
em.close();
}
//List<Video[]> videos = q.getResultList();
//System.out.println(video);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment