Skip to content

Commit

Permalink
feat: allow source lang selection for TMX export
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Jun 21, 2017
1 parent fa8f01c commit 5360ec1
Show file tree
Hide file tree
Showing 18 changed files with 796 additions and 4 deletions.
@@ -0,0 +1,73 @@
package org.zanata.rest.dto;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.io.Serializable;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
@XmlType(name = "sourceLocaleDetailsType")
@XmlRootElement(name = "sourceLocaleDetails")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonPropertyOrder({"docCount", "localeDetails"})
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class SourceLocaleDetails implements Serializable {
private Integer docCount;
private LocaleDetails localeDetails;

public SourceLocaleDetails() {
this(null, null);
}

public SourceLocaleDetails(Integer docCount, LocaleDetails localeDetails) {
this.docCount = docCount;
this.localeDetails = localeDetails;
}

@XmlAttribute(name = "docCount", required = true)
public Integer getDocCount() {
return docCount;
}

public void setDocCount(Integer docCount) {
this.docCount = docCount;
}

@XmlAttribute(name = "localeDetails", required = true)
public LocaleDetails getLocaleDetails() {
return localeDetails;
}

public void setLocaleDetails(LocaleDetails localeDetails) {
this.localeDetails = localeDetails;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SourceLocaleDetails details = (SourceLocaleDetails) o;

if (docCount != null ? !docCount.equals(details.docCount) :
details.docCount != null) return false;
return localeDetails != null ?
localeDetails.equals(details.localeDetails) :
details.localeDetails == null;
}

@Override
public int hashCode() {
int result = docCount != null ? docCount.hashCode() : 0;
result =
31 * result +
(localeDetails != null ? localeDetails.hashCode() : 0);
return result;
}
}
Expand Up @@ -12,9 +12,12 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import org.zanata.rest.dto.LocaleDetails;
import org.zanata.rest.dto.LocaleMember;
import org.zanata.rest.dto.SourceLocaleDetails;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
Expand Down Expand Up @@ -74,6 +77,21 @@ Response get(@QueryParam("filter") String filter,
@TypeHint(LocaleDetails[].class)
Response getUITranslations();

/**
* Returns list of active source locales of all documents for all active projects.
*
* @return
* OK 200 containing the list of SourceLocaleDetails
*/
@GET
@StatusCodes({
@ResponseCode(code = 200, condition = "Content contains a list of source locale details")
})
@Produces(MediaType.APPLICATION_JSON)
@Path("/source")
@TypeHint(SourceLocaleDetails[].class)
Response getSourceLocales();


/**
* Retrieves a list of locales that is not added yet in the server.
Expand Down
Expand Up @@ -28,10 +28,13 @@
import javax.ws.rs.core.Response;

import com.webcohesion.enunciate.metadata.rs.ResourceLabel;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import org.zanata.rest.MediaTypes;
import org.zanata.rest.dto.LocaleDetails;

import com.webcohesion.enunciate.metadata.rs.TypeHint;
import org.zanata.rest.dto.SourceLocaleDetails;

/**
* REST interface for configured version locales.
Expand Down Expand Up @@ -61,4 +64,20 @@ public interface ProjectIterationLocalesResource extends RestResource {
MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response get();

/**
* Returns list of active source locales of all documents for a single project-version.
*
* @return
* OK 200 containing the list of SourceLocaleDetails
* NOT FOUND 404 if the project does not exist
*/
@GET
@StatusCodes({
@ResponseCode(code = 200, condition = "Content contains a list of source locale details"),
@ResponseCode(code = 404, condition = "The project is not found"),
})
@TypeHint(SourceLocaleDetails[].class)
@Path("/source")
public Response getSourceLocales();

}
Expand Up @@ -34,6 +34,7 @@
import org.zanata.rest.dto.LocaleDetails;

import com.webcohesion.enunciate.metadata.rs.TypeHint;
import org.zanata.rest.dto.SourceLocaleDetails;

/**
* REST interface for configured project locales.
Expand Down Expand Up @@ -66,4 +67,20 @@ public interface ProjectLocalesResource extends RestResource {
})
public Response get();

/**
* Returns list of active source locales of all documents for a single project.
*
* @return
* OK 200 containing the list of SourceLocaleDetails
* NOT FOUND 404 if the project does not exist
*/
@GET
@StatusCodes({
@ResponseCode(code = 200, condition = "Content contains a list of source locale details"),
@ResponseCode(code = 404, condition = "The project is not found"),
})
@TypeHint(SourceLocaleDetails[].class)
@Path("/source")
public Response getSourceLocales();

}
@@ -0,0 +1,68 @@
package org.zanata.rest.dto;

import org.junit.Test;
import org.zanata.common.LocaleId;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

/**
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*/
public class SourceLocaleDetailsTest {
@Test
public void testEmptyConstructor() {
SourceLocaleDetails details = new SourceLocaleDetails();
assertThat(details.getDocCount(), equalTo(null));
assertThat(details.getLocaleDetails(), equalTo(null));
}

@Test
public void testConstructor() {
LocaleDetails localeDetails =
new LocaleDetails(LocaleId.DE, "German", "", "", true,
false, "");
SourceLocaleDetails details =
new SourceLocaleDetails(10, localeDetails);

assertThat(details.getDocCount(), equalTo(10));
assertThat(details.getLocaleDetails(), equalTo(localeDetails));
}

@Test
public void testGetterSetter() {
SourceLocaleDetails details = new SourceLocaleDetails();
details.setDocCount(100);
assertThat(details.getDocCount(), equalTo(100));

LocaleDetails localeDetails =
new LocaleDetails(LocaleId.DE, "German", "", "", true,
false, "");
details.setLocaleDetails(localeDetails);
assertThat(details.getDocCount(), equalTo(localeDetails));

}

@Test
public void testEqualsAndHashCode() {
LocaleDetails localeDetails =
new LocaleDetails(LocaleId.DE, "German", "", "", true,
false, "");
SourceLocaleDetails details1 =
new SourceLocaleDetails(10, localeDetails);

SourceLocaleDetails details2 =
new SourceLocaleDetails(10, localeDetails);

assertThat(details1.hashCode(), equalTo(details2.hashCode()));
assertThat(details1.equals(details2), equalTo(true));

details2 =
new SourceLocaleDetails(100, localeDetails);

assertThat(details1.hashCode(), not(equalTo(details2.hashCode())));
assertThat(details1.equals(details2), equalTo(false));

}
}
86 changes: 86 additions & 0 deletions server/zanata-frontend/src/frontend/app/actions/tmx-actions.js
@@ -0,0 +1,86 @@
import { createAction } from 'redux-actions'
import { CALL_API } from 'redux-api-middleware'
import { isUndefined, includes } from 'lodash'
import {
getJsonHeaders,
buildAPIRequest,
} from './common-actions'
import { apiUrl } from '../config'

export const TMX_TYPE = ['all', 'project', 'version']

export const SHOW_EXPORT_TMX_MODAL = Symbol('SHOW_EXPORT_TMX_MODAL')
export const TOGGLE_SHOW_SOURCE_LANGUAGES = Symbol('TOGGLE_SHOW_SOURCE_LANGUAGES')

export const GET_LOCALE_REQUEST = Symbol('GET_LOCALE_REQUEST')
export const GET_LOCALE_SUCCESS = Symbol('GET_LOCALE_SUCCESS')
export const GET_LOCALE_FAILURE = Symbol('GET_LOCALE_FAILURE')

export const showExportTMXModal = createAction(SHOW_EXPORT_TMX_MODAL)
export const toggleShowSourceLanguages = createAction(TOGGLE_SHOW_SOURCE_LANGUAGES)

const fetchSourceLanguages = (endpoint) => {
const apiTypes = [
GET_LOCALE_REQUEST,
{
type: GET_LOCALE_SUCCESS,
payload: (action, state, res) => {
const contentType = res.headers.get('Content-Type')
if (contentType && includes(contentType, 'json')) {
return res.json().then((json) => {
return json
})
}
},
meta: {
receivedAt: Date.now()
}
},
GET_LOCALE_FAILURE
]
return {
[CALL_API]: buildAPIRequest(endpoint, 'GET', getJsonHeaders(), apiTypes)
}
}

// get all source language in all active documents
const fetchAllSourceLanguages = () => {
const endpoint = apiUrl + '/locales/source'
return fetchSourceLanguages(endpoint)
}

// get all source language in all active documents in project
const fetchProjectSourceLanguages = (project) => {
const endpoint = apiUrl + '/projects/p/' + project + '/locales/source'
return fetchSourceLanguages(endpoint)
}

// get all source language in all active documents in project version
const fetchVersionSourceLanguages = (project, version) => {
const endpoint = apiUrl + '/projects/p/' + project + '/iteration/i/' + version + '/locales/source'
return fetchSourceLanguages(endpoint)
}

export const showSourceLanguages = (show, type) => {
return (dispatch, getState) => {
toggleShowSourceLanguages(show)
if (show && isUndefined(getState().tmx.sourceLanguages)) {
if (type === 'all') {
dispatch(fetchAllSourceLanguages())
} else if (type === 'project') {
dispatch(fetchProjectSourceLanguages(getState().tmx.projectName))
} else {
dispatch(fetchVersionSourceLanguages(getState().tmx.projectName,
getState().tmx.version))
}
}
}
}

export const exportTMX = () => {
return (dispatch, getState) => {

}
}


0 comments on commit 5360ec1

Please sign in to comment.