Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Glossary REST API/Hibernate
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Aug 16, 2011
1 parent 25cc27e commit 2eb0268
Show file tree
Hide file tree
Showing 35 changed files with 594 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String toString()

public static final String APPLICATION_ZANATA_GLOSSARY = APPLICATION_VND_ZANATA + ".glossary";
public static final String APPLICATION_ZANATA_GLOSSARY_XML = APPLICATION_ZANATA_GLOSSARY + XML;
public static final String APPLICATION_ZANATA_GLOSSARY_JSON = APPLICATION_ZANATA_GLOSSARY + XML;
public static final String APPLICATION_ZANATA_GLOSSARY_JSON = APPLICATION_ZANATA_GLOSSARY + JSON;

/**
* Creates a format specific MediaType string given an existing media type
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.zanata.rest.client;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.jboss.resteasy.client.ClientResponse;
import org.zanata.common.LocaleId;
import org.zanata.rest.MediaTypes;
import org.zanata.rest.dto.Glossary;
import org.zanata.rest.dto.GlossaryTerm;

@Produces({ MediaTypes.APPLICATION_ZANATA_GLOSSARY_XML, MediaTypes.APPLICATION_ZANATA_GLOSSARY_JSON })
@Consumes({ MediaTypes.APPLICATION_ZANATA_GLOSSARY_XML, MediaTypes.APPLICATION_ZANATA_GLOSSARY_JSON })
public interface IGlossaryResource
{
public static final String SERVICE_PATH = "/glossary";

@GET
@Path(SERVICE_PATH)
public ClientResponse<Glossary> getEntries();

@GET
@Path(SERVICE_PATH + "/{locale}")
public ClientResponse<Glossary> get(@PathParam("locale") LocaleId locale);

@PUT
@Path(SERVICE_PATH)
public ClientResponse<Glossary> put(Glossary glossary);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
Expand All @@ -46,13 +45,16 @@ public interface GlossaryResource
{
public static final String SERVICE_PATH = "/glossary";

@GET
@Path("SERVICE_PATH")
public Response getEntries();

@GET
@Path(SERVICE_PATH + "/{locale}")
@Produces({ MediaTypes.APPLICATION_ZANATA_GLOSSARY_XML, MediaTypes.APPLICATION_ZANATA_GLOSSARY_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response get(@PathParam("locale") LocaleId locale);

@PUT
@Consumes({ MediaTypes.APPLICATION_ZANATA_GLOSSARY_XML, MediaTypes.APPLICATION_ZANATA_GLOSSARY_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("SERVICE_PATH")
public Response put(InputStream messageBody);

}
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ public void serializeAndDeserializeGlossary() throws JsonGenerationException, Js
entry.getGlossaryTerms().add(term2);
glossary.getGlossaryEntries().add(entry);

System.out.println(glossary.toString());

System.out.println(glossary);
JaxbUtil.validateXml(glossary, Glossary.class);
String output = mapper.writeValueAsString(glossary);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.NaturalId;

/**
*
Expand All @@ -38,16 +41,18 @@
@Entity
public class HGlossaryEntry extends ModelEntityBase
{

private Map<HLocale, HGlossaryTerm> glossaryTerms;

private HGlossaryTerm srcTerm;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "glossaryEntry")
@MapKey(name = "locale")
@BatchSize(size = 10)
public Map<HLocale, HGlossaryTerm> getGlossaryTerms()
{
if (glossaryTerms == null)
glossaryTerms = new HashMap<HLocale, HGlossaryTerm>();

return glossaryTerms;
}

Expand All @@ -56,6 +61,19 @@ public void setGlossaryTerms(Map<HLocale, HGlossaryTerm> glossaryTerms)
this.glossaryTerms = glossaryTerms;
}

@OneToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "srcTermId")
@NaturalId
public HGlossaryTerm getSrcTerm()
{
return srcTerm;
}

public void setSrcTerm(HGlossaryTerm srcTerm)
{
this.srcTerm = srcTerm;
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package org.zanata.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
Expand Down Expand Up @@ -52,6 +53,11 @@ public class HGlossaryTerm extends ModelEntityBase
private HGlossaryEntry glossaryEntry;
private HLocale locale;

public HGlossaryTerm()
{

}

public HGlossaryTerm(String content)
{
setContent(content);
Expand Down Expand Up @@ -86,6 +92,10 @@ public void setSourceRef(String refs)
@JoinColumn(name = "glossaryTermId", nullable = false)
public List<HTermComment> getComments()
{
if (comments == null)
{
comments = new ArrayList<HTermComment>();
}
return comments;
}

Expand All @@ -95,7 +105,7 @@ public void setComments(List<HTermComment> comments)
}

@ManyToOne
@JoinColumn(name = "glossaryEntryId", nullable = false)
@JoinColumn(name = "glossaryEntryId")
@NaturalId
public HGlossaryEntry getGlossaryEntry()
{
Expand All @@ -116,13 +126,11 @@ public HLocale getLocale()
{
return locale;
}

public void setLocale(HLocale locale)
{
this.locale = locale;
}


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class HTermComment

private Integer pos;

private boolean obsolete = false;

private HGlossaryTerm glossaryTerm;

public HTermComment()
Expand Down Expand Up @@ -91,6 +93,16 @@ public void setPos(Integer pos)
this.pos = pos;
}

public boolean isObsolete()
{
return obsolete;
}

public void setObsolete(boolean obsolete)
{
this.obsolete = obsolete;
}

@ManyToOne
@JoinColumn(name = "glossaryTermId", insertable = false, updatable = false, nullable = false)
@NaturalId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.common.LocaleId;
Expand All @@ -39,40 +40,55 @@
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
**/
@Name("glossaryTermDAO")
@Name("glossaryEntryDAO")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class GlossaryTermDAO extends AbstractDAOImpl<HGlossaryTerm, Long>
public class GlossaryDAO extends AbstractDAOImpl<HGlossaryEntry, Long>
{
public GlossaryDAO()
{
super(HGlossaryEntry.class);
}

public GlossaryDAO(Session session)
{
super(HGlossaryEntry.class, session);
}

public GlossaryTermDAO()
public HGlossaryEntry getEntryById(Long id)
{
super(HGlossaryTerm.class);
return (HGlossaryEntry) getSession().createCriteria(HGlossaryEntry.class).add(Restrictions.naturalId().set("id", id)).setCacheable(true).setComment("GlossaryDAO.getEntryById").uniqueResult();
}

public GlossaryTermDAO(Session session)
public HGlossaryTerm getTermByEntryAndLocale(Long glossaryEntryId, LocaleId locale)
{
super(HGlossaryTerm.class, session);
Query query = getSession().createQuery("from HGlossaryTerm as t WHERE t.locale.localeId= :locale AND glossaryEntry.id= :glossaryEntryId");
query.setParameter("locale", locale);
query.setParameter("glossaryEntryId", glossaryEntryId);
return (HGlossaryTerm) query.uniqueResult();
}

public HGlossaryTerm findByNaturalId(HGlossaryEntry glossaryEntry, HLocale locale)
@SuppressWarnings("unchecked")
public List<HGlossaryTerm> getTermByGlossaryEntryId(Long glossaryEntryId)
{
return (HGlossaryTerm) getSession().createCriteria(HGlossaryTerm.class).add(Restrictions.naturalId().set("glossaryEntry", glossaryEntry).set("locale", locale)).setCacheable(true).setComment("GlossaryTermDAO.findByNaturalId").uniqueResult();
Query query = getSession().createQuery("from HGlossaryTerm as t WHERE t.glossaryEntry.id= :glossaryEntryId");
query.setParameter("glossaryEntryId", glossaryEntryId);
return query.list();

}

@SuppressWarnings("unchecked")
public List<HGlossaryTerm> findByGlossaryEntryId(HGlossaryEntry glossaryEntry)
public List<HGlossaryEntry> getEntriesByLocaleId(LocaleId locale)
{
Query query = getSession().createQuery("from HTerm as t where t.glossaryEntryId= :glossaryEntryId");
query.setParameter("glossaryEntryId", glossaryEntry.getId());
Query query = getSession().createQuery("from HGlossaryEntry as e WHERE e.id IN (SELECT t.glossaryEntry.id FROM HGlossaryTerm as t WHERE t.locale.localeId= :localeId)");
query.setParameter("localeId", locale);
return query.list();
}

@SuppressWarnings("unchecked")
public List<HGlossaryTerm> findByLocaleId(LocaleId locale)
public List<HGlossaryEntry> getEntries()
{
Query query = getSession().createQuery("from HTerm as t where t.localeId= :localeId");
query.setParameter("localeId", locale.getId());
Query query = getSession().createQuery("from HGlossaryEntry");
return query.list();
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public ETagUtils(Session session, DocumentDAO documentDAO)
this.documentDAO = documentDAO;
}

public ETagUtils(Session session)
{
this.session = session;
}

/**
* Retrieves the ETag for the Project
*
Expand Down Expand Up @@ -116,12 +121,12 @@ public EntityTag generateETagForDocument(HProjectIteration iteration, String id,
return EntityTag.valueOf(String.valueOf(hashcode));
}

public EntityTag generateTagForGlossary(List<Integer> glossaryEntryIds)
public EntityTag generateTagForGlossary(List<Long> glossaryEntryIds)
{
List<String> glossaryVersions = new ArrayList<String>();
for (int glossaryEntryId : glossaryEntryIds)
for (Long glossaryEntryId : glossaryEntryIds)
{
Integer glossaryId = (Integer) session.createQuery("select g.id from HGlossaryEntry g where id =:id").setParameter("id", glossaryEntryId).uniqueResult();
Long glossaryId = (Long) session.createQuery("select g.id from HGlossaryEntry g where g.id =:id").setParameter("id", glossaryEntryId).uniqueResult();
if (glossaryId == null)
{
throw new NoSuchEntityException("GlossaryEntry '" + glossaryEntryId + "' not found.");
Expand All @@ -134,7 +139,7 @@ public EntityTag generateTagForGlossary(List<Integer> glossaryEntryIds)

public EntityTag generateTagForGlossaryTerm(LocaleId locale)
{
Object[] queryResult = (Object[]) session.createQuery("select g.glossaryEntryId,g.localeId from HGlossaryTerm g where locale =:locale").setParameter("locale", new HLocale(locale)).uniqueResult();
Object[] queryResult = (Object[]) session.createQuery("select g.glossaryEntry.id,g.locale.id from HGlossaryTerm g where locale.localeId =:locale").setParameter("locale", locale).uniqueResult();
if (queryResult == null)
{
throw new NoSuchEntityException("HGlossaryTerm with locale '" + locale + "' not found.");
Expand Down

0 comments on commit 2eb0268

Please sign in to comment.