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

Commit

Permalink
Refactor Metadata tables.
Browse files Browse the repository at this point in the history
Metadata is now a single string in both the TU and TUVs.
Change the way the Translation Memory size is being calculated in the home page. It's now done using a query.
Add pages.xml entry for the Trans Memory admin home page.
  • Loading branch information
carlosmunoz committed Aug 5, 2013
1 parent f3592a8 commit 366c3b0
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 85 deletions.
Expand Up @@ -80,7 +80,7 @@ public static TransMemory tm(String slug)
*/
@ElementCollection
@MapKeyEnumerated(EnumType.STRING)
@MapKeyColumn(name = "metadata_key")
@MapKeyColumn(name = "metadata_type")
@JoinTable(name = "TransMemory_Metadata", joinColumns = {@JoinColumn(name = "trans_memory_id")})
@Column(name = "metadata",length = Integer.MAX_VALUE)
private Map<TMMetadataType, String> metadata = Maps.newHashMap();
Expand Down
Expand Up @@ -20,6 +20,7 @@
*/
package org.zanata.model.tm;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.Access;
import javax.persistence.AccessType;
Expand All @@ -28,6 +29,7 @@
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
Expand Down Expand Up @@ -106,15 +108,12 @@ public TransMemoryUnit(TransMemory tm, String uniqueId, String transUnitId, Stri
@IndexedEmbedded
private Map<String, TransMemoryUnitVariant> transUnitVariants = Maps.newHashMap();

/**
* Map values are Json strings containing metadata for the particular type of translation memory
*/
@ElementCollection
@MapKeyEnumerated(EnumType.STRING)
@MapKeyColumn(name = "metadata_key")
@JoinTable(name = "TransMemoryUnit_Metadata", joinColumns = {@JoinColumn(name = "tm_trans_unit_id")})
@Column(name = "metadata", length = Integer.MAX_VALUE)
private Map<TMMetadataType, String> metadata = Maps.newHashMap();
@Enumerated(EnumType.STRING)
@Column(name = "metadata_type", nullable = true)
private TMMetadataType metadataType;

@Column(nullable = true)
private String metadata;

public TransMemoryUnit(String uniqueId)
{
Expand All @@ -126,4 +125,15 @@ protected boolean logPersistence()
{
return false;
}

@Override
public Map<TMMetadataType, String> getMetadata()
{
HashMap<TMMetadataType,String> metadata = Maps.newHashMap();

This comment has been minimized.

Copy link
@seanf

seanf Aug 5, 2013

Contributor

I have already changed this method to accept a TMMetadataType and return a String, but just for future reference:

This method returns a "disconnected" HashMap, ie changing the HashMap no longer affects the persistent entity. (In fact there is no public method which allows changing the metadata.)

We had some code in TMXMetadataHelper which was calling getMetadata().put(...), which began to fail silently. If this method had returned a Guava ImmutableMap, any caller which attempted to modify the map would get an immediate exception instead of a silent failure.

if( metadataType != null )
{
metadata.put(this.metadataType, this.metadata);
}
return metadata;
}
}
Expand Up @@ -20,13 +20,15 @@
*/
package org.zanata.model.tm;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyColumn;
Expand Down Expand Up @@ -81,15 +83,12 @@ public class TransMemoryUnitVariant extends ModelEntityBase implements HasTMMeta
@Column(name ="plain_text_segment_hash", nullable = false)
private String plainTextSegmentHash;

/**
* Map values are Json strings containing metadata for the particular type of translation memory
*/
@ElementCollection
@JoinTable(name = "TransMemoryUnitVariant_Metadata", joinColumns = {@JoinColumn(name = "tm_trans_unit_variant_id")})
@MapKeyEnumerated(EnumType.STRING)
@MapKeyColumn(name = "metadata_key")
@Column(name = "metadata", length = Integer.MAX_VALUE)
private Map<TMMetadataType, String> metadata = Maps.newHashMap();
@Enumerated(EnumType.STRING)
@Column(name = "metadata_type", nullable = true)
private TMMetadataType metadataType;

@Column(nullable = true)
private String metadata;

public static TransMemoryUnitVariant tuv(String language, String content)
{
Expand Down Expand Up @@ -125,6 +124,17 @@ protected boolean logPersistence()
return false;
}

@Override
public Map<TMMetadataType, String> getMetadata()
{
HashMap<TMMetadataType,String> metadata = Maps.newHashMap();
if( metadataType != null )
{
metadata.put(this.metadataType, this.metadata);
}
return metadata;
}

public static Map<String, TransMemoryUnitVariant> newMap(TransMemoryUnitVariant... tuvs)
{
Map<String, TransMemoryUnitVariant> map = Maps.newHashMap();
Expand Down
Expand Up @@ -80,6 +80,11 @@ public void clearTransMemory(String transMemorySlug)
transMemoryList = null; // Force refresh next time list is requested
}

public long getTranslationMemorySize(String tmSlug)
{
return transMemoryDAO.getTranslationMemorySize(tmSlug);
}

public String cancel()
{
// Navigation logic in pages.xml
Expand Down
9 changes: 9 additions & 0 deletions zanata-war/src/main/java/org/zanata/dao/TransMemoryDAO.java
Expand Up @@ -27,6 +27,7 @@

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
Expand Down Expand Up @@ -93,4 +94,12 @@ public void deleteTransMemoryContents(@Nonnull String slug)
.setCacheable(false)
.uniqueResult();
}

public long getTranslationMemorySize(@Nonnull String tmSlug)
{
return (Long)getSession()
.createQuery("select count(tu) from TransMemoryUnit tu where tu.translationMemory.slug = :tmSlug")
.setString("tmSlug", tmSlug)
.uniqueResult();
}
}
2 changes: 1 addition & 1 deletion zanata-war/src/main/java/org/zanata/tmx/TMXParser.java
Expand Up @@ -111,7 +111,7 @@ else if (elem.getName().equals(header))
}
}
}
commitBatch(handledTUs, false);
commitBatch(handledTUs, true); // A new transaction is needed for Seam to commit it
}
catch (EntityExistsException e)
{
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.transaction.Transaction;
import org.zanata.common.LocaleId;
import org.zanata.dao.TransMemoryDAO;
import org.zanata.model.tm.TransMemoryUnitVariant;
Expand Down
86 changes: 36 additions & 50 deletions zanata-war/src/main/resources/db/changelogs/db.changelog-3.1.xml
Expand Up @@ -7,7 +7,7 @@
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

<changeSet id="1" author="camunoz@redhat.com">
<comment>Create Translation Memory tables.</comment>
<comment>Create the Translation Memory table.</comment>

<createTable tableName="TransMemory">
<column name="description" type="longtext">
Expand All @@ -33,7 +33,29 @@
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

<changeSet id="2" author="camunoz@redhat.com">
<comment>Create Translation Memory metadata table.</comment>

<createTable tableName="TransMemory_Metadata">
<column name="trans_memory_id" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="metadata_type" type="varchar(75)">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="metadata" type="longtext"/>
</createTable>

<addForeignKeyConstraint baseTableName="TransMemory_Metadata" baseColumnNames="trans_memory_id"
constraintName="FK_Metadata_TransMemory"
referencedTableName="TransMemory"
referencedColumnNames="id"/>
</changeSet>

<changeSet id="3" author="camunoz@redhat.com">
<comment>Create the TransMemoryUnit table.</comment>
<createTable tableName="TransMemoryUnit">
<column name="trans_unit_id" type="longtext">
<constraints nullable="true"/>
Expand All @@ -42,14 +64,16 @@
<constraints nullable="true"/>
</column>
<column name="tm_id" type="bigint">
<constraints nullable="false" uniqueConstraintName="UK_natural_id"/>
<constraints nullable="false"/>
</column>
<column name="unique_id" type="varchar(255)">
<constraints nullable="false" uniqueConstraintName="UK_natural_id"/>
<constraints nullable="false"/>
</column>
<column name="position" type="integer">
<constraints nullable="true"/>
</column>
<column name="metadata_type" type="varchar(75)"/>
<column name="metadata" type="longtext"/>
<!-- Base entity columns -->
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
Expand All @@ -65,10 +89,16 @@
</column>
</createTable>

<addForeignKeyConstraint baseTableName="TransMemoryUnit" baseColumnNames="tm_id" constraintName="FK_tmunit_trans_memory"
<addUniqueConstraint constraintName="UK_natural_id"
tableName="TransMemoryUnit" columnNames="tm_id,unique_id" />
<addForeignKeyConstraint baseTableName="TransMemoryUnit" baseColumnNames="tm_id"
constraintName="FK_tmunit_trans_memory"
referencedTableName="TransMemory"
referencedColumnNames="id"/>
</changeSet>

<changeSet id="4" author="camunoz@redhat.com">
<comment>Create the TransMemoryUnitVariant table.</comment>
<createTable tableName="TransMemoryUnitVariant">
<column name="language" type="varchar(255)">
<constraints nullable="false"/>
Expand All @@ -85,6 +115,8 @@
<column name="plain_text_segment_hash" type="char(32)">
<constraints nullable="false"/>
</column>
<column name="metadata_type" type="varchar(75)"/>
<column name="metadata" type="longtext"/>
<!-- Base entity columns -->
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
Expand All @@ -106,52 +138,6 @@
referencedColumnNames="id"/>
</changeSet>

<changeSet id="2" author="camunoz@redhat.com">
<comment>Create Translation Memory metadata tables.</comment>

<createTable tableName="TransMemory_Metadata">
<column name="trans_memory_id" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="metadata_key" type="varchar(75)">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="metadata" type="longtext"/>
</createTable>
<createTable tableName="TransMemoryUnit_Metadata">
<column name="tm_trans_unit_id" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="metadata_key" type="varchar(75)">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="metadata" type="longtext"/>
</createTable>
<createTable tableName="TransMemoryUnitVariant_Metadata">
<column name="tm_trans_unit_variant_id" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="metadata_key" type="varchar(75)">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="metadata" type="longtext"/>
</createTable>

<addForeignKeyConstraint baseTableName="TransMemory_Metadata" baseColumnNames="trans_memory_id"
constraintName="FK_Metadata_TransMemory"
referencedTableName="TransMemory"
referencedColumnNames="id"/>
<addForeignKeyConstraint baseTableName="TransMemoryUnit_Metadata" baseColumnNames="tm_trans_unit_id"
constraintName="FK_Metadata_TMTransUnit"
referencedTableName="TransMemoryUnit"
referencedColumnNames="id"/>
<addForeignKeyConstraint baseTableName="TransMemoryUnitVariant_Metadata" baseColumnNames="tm_trans_unit_variant_id"
constraintName="FK_Metadata_TransMemoryUnitVariant"
referencedTableName="TransMemoryUnitVariant"
referencedColumnNames="id"/>

</changeSet>

<changeSet id="1" author="pahuang@redhat.com">
<comment>Add HTextFlowTargetReviewComment table to support user comments.</comment>
<createTable tableName="HTextFlowTargetReviewComment">
Expand Down
27 changes: 17 additions & 10 deletions zanata-war/src/main/webapp/WEB-INF/pages.xml
Expand Up @@ -895,18 +895,25 @@
</page>

<!-- Translation Memory -->
<page view-id="/tm/home.xhtml">
<restrict>#{s:hasRole('admin')}</restrict>
<action execute="#{breadcrumbs.clear}"/>
<action execute="#{breadcrumbs.addLocation('/tm/home.xhtml', messages['jsf.transmemory.Title'])}"/>
</page>

<page view-id="/tm/create.xhtml">
<restrict>#{s:hasRole('admin')}</restrict>
<action execute="#{breadcrumbs.clear}"/>
<action execute="#{breadcrumbs.addLocation('/tm/home.xhtml', messages['jsf.transmemory.Title'])}"/>
<action execute="#{breadcrumbs.addLocation('', messages['jsf.transmemory.title.New'])}"/>
<navigation from-action="#{translationMemoryAction.persist}">
<redirect view-id="/tm/home.xhtml"/>
</navigation>
<navigation from-action="#{translationMemoryAction.cancel}">
<redirect view-id="/tm/home.xhtml" />
</navigation>
<restrict>#{s:hasRole('admin')}</restrict>
<action execute="#{breadcrumbs.clear}"/>
<action execute="#{breadcrumbs.addLocation('/tm/home.xhtml', messages['jsf.transmemory.Title'])}"/>
<action execute="#{breadcrumbs.addLocation('', messages['jsf.transmemory.title.New'])}"/>
<navigation from-action="#{translationMemoryAction.persist}">
<redirect view-id="/tm/home.xhtml"/>
</navigation>
<navigation from-action="#{translationMemoryAction.cancel}">
<redirect view-id="/tm/home.xhtml" />
</navigation>
</page>

<!-- Exceptions -->
<page view-id="/error.xhtml">
<action execute="#{breadcrumbs.clear}"/>
Expand Down
7 changes: 4 additions & 3 deletions zanata-war/src/main/webapp/tm/home.xhtml
Expand Up @@ -51,11 +51,12 @@
if (req.readyState != 4) { return; }
if (req.status != 200) {
alert("There was an error uploading the file: " + req.statusText + " (" + req.status + ")");
return;
}
else {
alert("Successfully imported the file");
}
resetUploadDialog();
refreshDataTable();
};
var file = uploadForm.find("input[name = 'uploadedFile']")[0].files[0];
req.send(file);
Expand Down Expand Up @@ -115,7 +116,7 @@
</rich:column>
<rich:column>
<f:facet name="header">#{messages['jsf.transmemory.NoOfEntries']}</f:facet>
<h:outputText value="#{tm.translationUnits.size()}" />
<h:outputText value="#{translationMemoryAction.getTranslationMemorySize(tm.slug)}" />
</rich:column>
<rich:column >
<f:facet name="header">#{messages['jsf.CreationDate']}
Expand All @@ -136,7 +137,7 @@
<a4j:commandLink value="#{messages['jsf.Clear']}"
action="#{translationMemoryAction.clearTransMemory(tm.slug)}"
render="tmList"
rendered="#{tm.translationUnits.size() gt 0}"
rendered="#{translationMemoryAction.getTranslationMemorySize(tm.slug) gt 0}"
onclick="return confirmClearTm()"/>
</rich:column>
</rich:dataTable>
Expand Down
Expand Up @@ -33,8 +33,6 @@
<HTextFlowTargetHistory/>
<HTextFlowTargetReviewComment/>
<TransMemory_Metadata/>
<TransMemoryUnit_Metadata/>
<TransMemoryUnitVariant_Metadata/>
<TransMemory/>
<TransMemoryUnit/>
<TransMemoryUnitVariant/>
Expand Down

0 comments on commit 366c3b0

Please sign in to comment.