This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ConfigurationServiceImpl.java
162 lines (144 loc) · 5.81 KB
/
ConfigurationServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright 2010, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
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.ApplicationConfiguration;
import org.zanata.common.Namespaces;
import org.zanata.common.ProjectType;
import org.zanata.dao.ProjectIterationDAO;
import org.zanata.model.HLocale;
import org.zanata.model.HProjectIteration;
import org.zanata.service.ConfigurationService;
import org.zanata.service.LocaleService;
@Name("configurationServiceImpl")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class ConfigurationServiceImpl implements ConfigurationService
{
private static final String FILE_NAME = "zanata.xml";
private static final String PROJECT_TYPE_OFFLINE_PO = "offlinepo";
@In
private LocaleService localeServiceImpl;
@In
private ProjectIterationDAO projectIterationDAO;
@In
private ApplicationConfiguration applicationConfiguration;
@Override
public String getConfigurationFileContents(String projectSlug, String iterationSlug, boolean useOfflinePo)
{
return getConfigurationFileContents(projectSlug, iterationSlug, useOfflinePo, applicationConfiguration.getServerPath());
}
@Override
public String getConfigurationFileContents(String projectSlug, String iterationSlug, HLocale locale, boolean useOfflinePo)
{
return getConfigurationFileContents(projectSlug, iterationSlug, locale, useOfflinePo, applicationConfiguration.getServerPath());
}
@Override
public String getConfigurationFileContents(String projectSlug, String iterationSlug, boolean useOfflinePo, String serverPath)
{
return getConfigurationFileContents(projectSlug, iterationSlug, null, useOfflinePo, serverPath);
}
@Override
public String getConfigurationFileContents(String projectSlug, String iterationSlug, HLocale locale, boolean useOfflinePo, String serverPath)
{
HProjectIteration projectIteration = projectIterationDAO.getBySlug(projectSlug, iterationSlug);
ProjectType projectType = projectIteration.getProjectType();
StringBuilder var = new StringBuilder(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<config xmlns=\"" + Namespaces.ZANATA_CONFIG + "\">\n");
var.append(" <url>").append(serverPath).append("/</url>\n");
var.append(" <project>").append(projectSlug).append("</project>\n");
var.append(" <project-version>").append(iterationSlug).append("</project-version>\n");
if (useOfflinePo)
{
// FIXME this comment could be localized
var.append(" <!-- NB project-type set to 'offlinepo' to allow offline po translation\n")
.append(" from non-po documents, project-type on server is '")
.append(String.valueOf(projectType).toLowerCase()).append("' -->\n")
.append(" <project-type>").append(PROJECT_TYPE_OFFLINE_PO).append("</project-type>\n");
}
else if ( projectType != null )
{
if( projectType == ProjectType.Gettext )
{
// FIXME this comment could be localized
var.append(" <!-- NB project-type set to 'podir' to allow uploads, but original was 'gettext' -->\n");
var.append(" <project-type>").append(ProjectType.Podir.toString().toLowerCase()).append("</project-type>\n");
}
else
{
var.append(" <project-type>").append(projectType.toString().toLowerCase()).append("</project-type>\n");
}
}
else
{
var.append(" <!--<project-type>");
var.append(StringUtils.join(ProjectType.values(), "|").toLowerCase());
var.append("</project-type>-->\n");
}
var.append("\n");
List<HLocale> locales;
if (locale == null)
{
locales = localeServiceImpl.getSupportedLangugeByProjectIteration(projectSlug, iterationSlug);
}
else
{
locales = new ArrayList<HLocale>();
locales.add(locale);
}
HLocale source = localeServiceImpl.getSourceLocale(projectSlug, iterationSlug);
if(locales!=null)
{
boolean first=true;
for(HLocale op: locales)
{
if(!op.equals(source))
{
if (first)
{
var.append(" <locales>\n");
}
var.append(" <locale>").append(op.getLocaleId().getId()).append("</locale>\n");
first = false;
}
}
if (!first)
{
var.append(" </locales>\n\n");
}
}
var.append("</config>\n");
return var.toString();
}
@Override
public String getConfigurationFileName()
{
return FILE_NAME;
}
}