~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/i18n/action/TranslateAction.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.i18n.action;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2005, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the <ORGANIZATION> nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
import java.util.Hashtable;
 
31
import java.util.List;
 
32
import java.util.Locale;
 
33
import java.util.Map;
 
34
 
 
35
import javax.servlet.http.HttpServletRequest;
 
36
 
 
37
import org.apache.commons.logging.Log;
 
38
import org.apache.commons.logging.LogFactory;
 
39
import org.hisp.dhis.i18n.I18nService;
 
40
import org.hisp.dhis.i18n.util.LocaleUtils;
 
41
 
 
42
import com.opensymphony.webwork.ServletActionContext;
 
43
import com.opensymphony.xwork.Action;
 
44
 
 
45
/**
 
46
 * @author Oyvind Brucker
 
47
 */
 
48
public class TranslateAction 
 
49
    implements Action
 
50
{
 
51
    private static final Log log = LogFactory.getLog( TranslateAction.class );
 
52
    
 
53
    private String className;
 
54
 
 
55
    private String id;
 
56
 
 
57
    private String loc;
 
58
 
 
59
    private String returnUrl;
 
60
 
 
61
    // -------------------------------------------------------------------------
 
62
    // Dependencies
 
63
    // -------------------------------------------------------------------------
 
64
 
 
65
    private I18nService i18nService;
 
66
 
 
67
    public void setI18nService( I18nService i18nService )
 
68
    {
 
69
        this.i18nService = i18nService;
 
70
    }
 
71
 
 
72
    // -------------------------------------------------------------------------
 
73
    // Input
 
74
    // -------------------------------------------------------------------------
 
75
 
 
76
    public void setClassName( String className )
 
77
    {
 
78
        this.className = className;
 
79
    }
 
80
 
 
81
    public void setId( String id )
 
82
    {
 
83
        this.id = id;
 
84
    }
 
85
 
 
86
    public void setLoc( String locale )
 
87
    {
 
88
        this.loc = locale;
 
89
    }
 
90
 
 
91
    public void setReturnUrl( String returnUrl )
 
92
    {
 
93
        this.returnUrl = returnUrl;
 
94
    }
 
95
 
 
96
    // -------------------------------------------------------------------------
 
97
    // Output
 
98
    // -------------------------------------------------------------------------
 
99
 
 
100
    public String getClassName()
 
101
    {
 
102
        return className;
 
103
    }
 
104
 
 
105
    public String getId()
 
106
    {
 
107
        return id;
 
108
    }
 
109
 
 
110
    public String getLocale()
 
111
    {
 
112
        return loc;
 
113
    }
 
114
 
 
115
    public String getReturnUrl()
 
116
    {
 
117
        return returnUrl;
 
118
    }
 
119
 
 
120
    // -------------------------------------------------------------------------
 
121
    // Action implementation
 
122
    // -------------------------------------------------------------------------
 
123
 
 
124
    public String execute()
 
125
        throws Exception
 
126
    {
 
127
        log.info( "Classname: " + className + ", id: " + id + ", loc: " + loc );
 
128
        
 
129
        HttpServletRequest request = ServletActionContext.getRequest();
 
130
 
 
131
        Map<String, String> translations = new Hashtable<String, String>();
 
132
 
 
133
        List<String> propertyNames = i18nService.getPropertyNames( className );
 
134
 
 
135
        for ( String propertyName : propertyNames )
 
136
        {
 
137
            String[] translation = request.getParameterValues( propertyName );
 
138
 
 
139
            if ( translation != null && translation.length > 0 && translation[0].length() > 0 )
 
140
            {
 
141
                translations.put( propertyName, translation[0] );
 
142
            }
 
143
        }
 
144
 
 
145
        log.info( "Translations: " + translations );
 
146
        
 
147
        Locale thisLocale = LocaleUtils.getLocale( loc );
 
148
 
 
149
        if ( thisLocale != null && !loc.equals( "heading" ) )
 
150
        {
 
151
            i18nService.updateTranslation( className, Integer.parseInt( id ), thisLocale, translations );
 
152
        }
 
153
 
 
154
        return SUCCESS;
 
155
    }
 
156
}