Java Code Examples for javax.persistence.TemporalType

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project odata4j, under directory /odata4j-core/src/main/java/org/odata4j/producer/jpa/.

Source file: JPAEdmGenerator.java

  23 
vote

protected EdmSimpleType<?> toEdmType(SingularAttribute<?,?> sa){
  Class<?> javaType=sa.getType().getJavaType();
  if (javaType.equals(Date.class) || javaType.equals(Calendar.class)) {
    TemporalType temporal=getTemporalType(sa);
    if (temporal == null) {
      return EdmSimpleType.DATETIME;
    }
 else {
switch (temporal) {
case DATE:
case TIMESTAMP:
        return EdmSimpleType.DATETIME;
case TIME:
      return EdmSimpleType.TIME;
  }
}
}
if (javaType.equals(Time.class)) {
return EdmSimpleType.TIME;
}
if (javaType.equals(Instant.class)) {
return EdmSimpleType.DATETIME;
}
if (javaType.equals(Timestamp.class)) {
return EdmSimpleType.DATETIME;
}
EdmSimpleType<?> rt=EdmSimpleType.forJavaType(javaType);
if (rt != null) return rt;
throw new UnsupportedOperationException(javaType.toString());
}
 

Example 2

From project CMM-data-grabber, under directory /paul/src/main/java/au/edu/uq/cmm/paul/queue/.

Source file: QueueManager.java

  19 
vote

public int expireAll(boolean discard,String facilityName,Slice slice,Date olderThan) throws InterruptedException {
  EntityManager em=createEntityManager();
  try {
    em.getTransaction().begin();
    String andPart="";
switch (slice) {
case HELD:
      andPart=" and m.userName is null";
    break;
case INGESTIBLE:
  andPart=" and m.userName is not null";
break;
}
if (facilityName != null && !facilityName.isEmpty()) {
andPart+=" and m.facilityName = :facility";
}
String queryString="from DatasetMetadata m " + "where m.updateTimestamp < :cutoff" + andPart;
TypedQuery<DatasetMetadata> query=em.createQuery(queryString,DatasetMetadata.class);
query.setParameter("cutoff",olderThan,TemporalType.TIMESTAMP);
if (facilityName != null && !facilityName.isEmpty()) {
query.setParameter("facility",facilityName);
}
List<DatasetMetadata> datasets=query.getResultList();
for (DatasetMetadata dataset : datasets) {
doDelete(discard,em,dataset);
}
em.getTransaction().commit();
return datasets.size();
}
  finally {
em.close();
}
}
 

Example 3

From project Core_2, under directory /javaee-impl/src/main/java/org/jboss/forge/spec/javaee/jpa/.

Source file: FieldPlugin.java

  19 
vote

@Command(value="temporal",help="Add a temporal field (java.util.Date) to an existing @Entity class") public void newTemporalField(@Option(name="type",required=true) final TemporalType temporalType,@Option(name="named",required=true,description="The field name",type=PromptType.JAVA_VARIABLE_NAME) final String fieldName){
  try {
    JavaClass entity=getJavaClass();
    addTemporalFieldTo(entity,Date.class,fieldName,temporalType);
  }
 catch (  FileNotFoundException e) {
    shell.println("Could not locate the @Entity requested. No update was made.");
  }
}
 

Example 4

From project Core_2, under directory /javaee-impl/src/main/java/org/jboss/forge/spec/javaee/jpa/.

Source file: FieldPlugin.java

  19 
vote

private Field<JavaClass> addTemporalFieldTo(final JavaClass targetEntity,final Class<?> fieldType,final String fieldName,final TemporalType temporalType) throws FileNotFoundException {
  if (targetEntity.hasField(fieldName)) {
    throw new IllegalStateException("Entity already has a field named [" + fieldName + "]");
  }
  JavaSourceFacet java=project.getFacet(JavaSourceFacet.class);
  Field<JavaClass> field=targetEntity.addField();
  field.setName(fieldName).setPrivate().setType(fieldType).addAnnotation(Temporal.class).setEnumValue(temporalType);
  if (!fieldType.getName().startsWith("java.lang.") && !fieldType.isPrimitive()) {
    targetEntity.addImport(fieldType);
  }
  Refactory.createGetterAndSetter(targetEntity,field);
  updateToString(targetEntity);
  java.saveJavaSource(targetEntity);
  shell.println("Added field to " + targetEntity.getQualifiedName() + ": "+ field);
  return field;
}
 

Example 5

From project Core_2, under directory /javaee-impl/src/test/java/org/jboss/forge/spec/jpa/.

Source file: NewFieldPluginTest.java

  19 
vote

@Test public void testNewTemporalField() throws Exception {
  Project project=getProject();
  JavaClass javaClass=generateEntity(project);
  getShell().execute(ConstraintInspector.getName(FieldPlugin.class) + " temporal --named time --type TIME");
  javaClass=(JavaClass)project.getFacet(JavaSourceFacet.class).getJavaResource(javaClass).getJavaSource();
  assertTrue(javaClass.hasField("time"));
  assertEquals("Date",javaClass.getField("time").getType());
  assertEquals(TemporalType.TIME,javaClass.getField("time").getAnnotation(Temporal.class).getEnumValue(TemporalType.class));
  assertTrue(javaClass.hasImport(TemporalType.class));
  assertTrue(javaClass.hasImport(Date.class));
  assertFalse(javaClass.hasSyntaxErrors());
}
 

Example 6

From project Empire, under directory /core/src/com/clarkparsia/empire/impl/.

Source file: RdfQuery.java

  19 
vote

/** 
 * @inheritDoc
 */
public Query setParameter(final String theName,final Calendar theCalendar,final TemporalType theTemporalType){
  validateParameterName(theName);
  Value aValue=asValue(theCalendar,theTemporalType);
  mNamedParameters.put(theName,aValue);
  return this;
}
 

Example 7

From project Empire, under directory /core/src/com/clarkparsia/empire/impl/.

Source file: RdfQuery.java

  19 
vote

/** 
 * Return the given date object with the specified temporal type as a  {@link Value}
 * @param theDate the date
 * @param theTemporalType the type to extract from the date
 * @return the time w.r.t to the TemportalType as a Value
 */
private Value asValue(final Calendar theDate,final TemporalType theTemporalType){
  Value aValue=null;
switch (theTemporalType) {
case DATE:
    aValue=ValueFactoryImpl.getInstance().createLiteral(Dates.date(theDate.getTime()),XMLSchema.DATE);
  break;
case TIME:
aValue=ValueFactoryImpl.getInstance().createLiteral(Dates.datetime(theDate.getTime()),XMLSchema.TIME);
break;
case TIMESTAMP:
aValue=ValueFactoryImpl.getInstance().createLiteral("" + theDate.getTime().getTime(),XMLSchema.TIME);
break;
}
return aValue;
}
 

Example 8

From project odata4j, under directory /odata4j-core/src/main/java/org/odata4j/producer/jpa/.

Source file: JPAEdmGenerator.java

  19 
vote

protected TemporalType getTemporalType(SingularAttribute<?,?> sa){
  Member member=sa.getJavaMember();
  Temporal temporal=null;
  if (member instanceof Field) {
    temporal=((Field)member).getAnnotation(Temporal.class);
  }
 else   if (member instanceof Method) {
    temporal=((Method)member).getAnnotation(Temporal.class);
  }
  return temporal == null ? null : temporal.value();
}
 

Example 9

From project OpenID-Connect-Java-Spring-Server, under directory /openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/.

Source file: JpaEventRepository.java

  19 
vote

@SuppressWarnings("unchecked") @Override @Transactional public Collection<Event> getEventsDuringPeriod(Date start,Date end,int startChunk,int chunkSize){
  Query query=manager.createQuery("SELECT e FROM Event e WHERE e.timestamp BETWEEN :start AND :end");
  query=query.setParameter("start",start,TemporalType.DATE);
  query=query.setParameter("end",end,TemporalType.DATE);
  query=query.setFirstResult(startChunk);
  query=query.setMaxResults(chunkSize);
  return query.getResultList();
}
 

Example 10

From project tawus, under directory /tawus-core/src/main/java/com/googlecode/tawus/internal/dataanalyzers/.

Source file: TimeAnalyzer.java

  19 
vote

public String identifyDataType(PropertyAdapter adapter){
  Class<?> clazz=adapter.getType();
  Temporal annotation=adapter.getAnnotation(Temporal.class);
  if (clazz.equals(Date.class) && annotation != null && annotation.value() == TemporalType.TIME) {
    return dataType;
  }
  return null;
}
 

Example 11

From project ticket2rock, under directory /src/test/java/de/ejb3buch/ticket2rock/session/.

Source file: AuskunftBeanIsolatedTest.java

  19 
vote

@Test public void findeKonzertMitZweiParametern(){
  Date datum=new Date();
  Mockito.when(em.createQuery("SELECT k FROM Konzert k where upper(k.ort.name) like :ortsName and k.datum >= :vonDatum")).thenReturn(query);
  Mockito.when(query.setParameter("ortsName","%HAMBURG%")).thenReturn(query);
  Mockito.when(query.setParameter("vonDatum",datum,TemporalType.DATE)).thenReturn(query);
  Mockito.when(query.getResultList()).thenReturn(konzerteInHamburg);
  AuskunftBean auskunft=gloss.make(AuskunftBean.class);
  List<Konzert> gefundeneKonzerte=auskunft.sucheKonzerte("Hamburg",datum,null);
  assertEquals(1,gefundeneKonzerte.size());
  assertEquals(konzertInHamburg,gefundeneKonzerte.get(0));
}