IDEMPIERE-810 Allow Translation Import from webui

This commit is contained in:
Carlos Ruiz 2013-03-28 18:01:59 -05:00
parent 4f6b64fff5
commit 6956b186a9
5 changed files with 231 additions and 59 deletions

View File

@ -0,0 +1,12 @@
-- Mar 19, 2013 2:48:21 PM COT
-- IDEMPIERE-810 Allow Translation Import from webui
INSERT INTO AD_Message (MsgType,MsgText,AD_Message_ID,EntityType,AD_Message_UU,Value,IsActive,Updated,CreatedBy,UpdatedBy,AD_Client_ID,AD_Org_ID,Created) VALUES ('I','Import ZIP',200156,'D','2f5186f7-44bf-4e1f-a7b7-a4061809eeae','ImportZIP','Y',TO_DATE('2013-03-19 14:48:20','YYYY-MM-DD HH24:MI:SS'),0,0,0,0,TO_DATE('2013-03-19 14:48:20','YYYY-MM-DD HH24:MI:SS'))
;
-- Mar 19, 2013 2:48:21 PM COT
INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy,AD_Message_Trl_UU ) SELECT l.AD_Language,t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy,Generate_UUID() FROM AD_Language l, AD_Message t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Message_ID=200156 AND NOT EXISTS (SELECT * FROM AD_Message_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.AD_Message_ID=t.AD_Message_ID)
;
SELECT register_migration_script('201303281606_IDEMPIERE-810.sql') FROM dual
;

View File

@ -0,0 +1,13 @@
-- Mar 19, 2013 2:48:21 PM COT
-- Mar 19, 2013 2:48:21 PM COT
INSERT INTO AD_Message (MsgType,MsgText,AD_Message_ID,EntityType,AD_Message_UU,Value,IsActive,Updated,CreatedBy,UpdatedBy,AD_Client_ID,AD_Org_ID,Created) VALUES ('I','Import ZIP',200156,'D','2f5186f7-44bf-4e1f-a7b7-a4061809eeae','ImportZIP','Y',TO_TIMESTAMP('2013-03-19 14:48:20','YYYY-MM-DD HH24:MI:SS'),0,0,0,0,TO_TIMESTAMP('2013-03-19 14:48:20','YYYY-MM-DD HH24:MI:SS'))
;
-- Mar 19, 2013 2:48:21 PM COT
-- I forgot to set the DICTIONARY_ID_COMMENTS System Configurator
INSERT INTO AD_Message_Trl (AD_Language,AD_Message_ID, MsgText,MsgTip, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy,AD_Message_Trl_UU ) SELECT l.AD_Language,t.AD_Message_ID, t.MsgText,t.MsgTip, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy,Generate_UUID() FROM AD_Language l, AD_Message t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Message_ID=200156 AND NOT EXISTS (SELECT * FROM AD_Message_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.AD_Message_ID=t.AD_Message_ID)
;
SELECT register_migration_script('201303281606_IDEMPIERE-810.sql') FROM dual
;

File diff suppressed because one or more lines are too long

View File

@ -14,6 +14,7 @@
package org.adempiere.webui.component; package org.adempiere.webui.component;
import java.io.File; import java.io.File;
import java.util.Arrays;
import org.adempiere.webui.apps.AEnv; import org.adempiere.webui.apps.AEnv;
import org.adempiere.webui.theme.ThemeManager; import org.adempiere.webui.theme.ThemeManager;
@ -132,6 +133,7 @@ public class FolderBrowser extends Window implements EventListener<Event>
} }
File[] files = dir.listFiles(); File[] files = dir.listFiles();
Arrays.sort(files);
for(File file: files) for(File file: files)
{ {

View File

@ -13,9 +13,19 @@
*****************************************************************************/ *****************************************************************************/
package org.adempiere.webui.install; package org.adempiere.webui.install;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.webui.LayoutUtils; import org.adempiere.webui.LayoutUtils;
import org.adempiere.webui.component.Button; import org.adempiere.webui.component.Button;
import org.adempiere.webui.component.FolderBrowser; import org.adempiere.webui.component.FolderBrowser;
@ -33,15 +43,20 @@ import org.adempiere.webui.panel.CustomForm;
import org.adempiere.webui.panel.IFormController; import org.adempiere.webui.panel.IFormController;
import org.adempiere.webui.panel.StatusBarPanel; import org.adempiere.webui.panel.StatusBarPanel;
import org.adempiere.webui.session.SessionManager; import org.adempiere.webui.session.SessionManager;
import org.adempiere.webui.util.ReaderInputStream;
import org.adempiere.webui.window.FDialog; import org.adempiere.webui.window.FDialog;
import org.apache.commons.io.FileUtils;
import org.compiere.install.Translation; import org.compiere.install.Translation;
import org.compiere.install.TranslationController; import org.compiere.install.TranslationController;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.KeyNamePair; import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg; import org.compiere.util.Msg;
import org.compiere.util.ValueNamePair; import org.compiere.util.ValueNamePair;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Borderlayout; import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Center; import org.zkoss.zul.Center;
import org.zkoss.zul.Div; import org.zkoss.zul.Div;
@ -84,6 +99,7 @@ public class WTranslationDialog extends TranslationController implements IFormCo
private Button bExport = new Button(); private Button bExport = new Button();
private Button bImport = new Button(); private Button bImport = new Button();
private Button bImportZIP = new Button();
private Label lClient = new Label(); private Label lClient = new Label();
private Listbox cbClient = ListboxFactory.newDropdownListbox(); private Listbox cbClient = ListboxFactory.newDropdownListbox();
@ -108,8 +124,12 @@ public class WTranslationDialog extends TranslationController implements IFormCo
// //
bExport.setLabel(Msg.getMsg(Env.getCtx(), "Export")); bExport.setLabel(Msg.getMsg(Env.getCtx(), "Export"));
bExport.addActionListener(this); bExport.addActionListener(this);
bExport.setStyle("text-align: right;");
bImport.setLabel(Msg.getMsg(Env.getCtx(), "Import")); bImport.setLabel(Msg.getMsg(Env.getCtx(), "Import"));
bImport.addActionListener(this); bImport.addActionListener(this);
bImportZIP.setLabel(Msg.getMsg(Env.getCtx(), "ImportZIP"));
bImportZIP.setUpload("true");
bImportZIP.addEventListener(Events.ON_UPLOAD, this);
Rows rows = centerLayout.newRows(); Rows rows = centerLayout.newRows();
Row row = rows.newRow(); Row row = rows.newRow();
@ -124,13 +144,21 @@ public class WTranslationDialog extends TranslationController implements IFormCo
row.appendChild(lTable.rightAlign()); row.appendChild(lTable.rightAlign());
row.appendChild(cbTable); row.appendChild(cbTable);
row = rows.newRow();
Div div = new Div(); Div div = new Div();
div.setStyle("text-align: right;"); div.setStyle("text-align: right;");
div.appendChild(bExport); div.appendChild(bExport);
row = rows.newRow();
row.appendChild(div); row.appendChild(div);
row.appendChild(bImport);
div = new Div();
div.setStyle("text-align: center;");
div.appendChild(bImport);
row.appendChild(div);
div = new Div();
div.setStyle("text-align: left;");
div.appendChild(bImportZIP);
row.appendChild(div);
} }
/** /**
@ -166,6 +194,10 @@ public class WTranslationDialog extends TranslationController implements IFormCo
SessionManager.getAppDesktop().closeActiveWindow(); SessionManager.getAppDesktop().closeActiveWindow();
} // dispose } // dispose
public ADForm getForm() {
return form;
}
/************************************************************************** /**************************************************************************
* Action Listener * Action Listener
* @param e event * @param e event
@ -192,7 +224,7 @@ public class WTranslationDialog extends TranslationController implements IFormCo
if (m_AD_Table == null) if (m_AD_Table == null)
return; return;
m_imp = (e.getTarget() == bImport); m_imp = (e.getTarget() == bImport || e.getTarget() == bImportZIP);
m_AD_Client_ID = -1; m_AD_Client_ID = -1;
KeyNamePair AD_Client = null; KeyNamePair AD_Client = null;
@ -202,16 +234,28 @@ public class WTranslationDialog extends TranslationController implements IFormCo
if (AD_Client != null) if (AD_Client != null)
m_AD_Client_ID = AD_Client.getKey(); m_AD_Client_ID = AD_Client.getKey();
if (e instanceof UploadEvent)
{
// upload and import zip
UploadEvent ue = (UploadEvent) e;
try {
processUploadMedia(ue.getMedia());
} catch (AdempiereException e1) {
throw new RuntimeException(e1);
}
return;
}
final FolderBrowser directoryDialog = new FolderBrowser(true); final FolderBrowser directoryDialog = new FolderBrowser(true);
directoryDialog.addEventListener(DialogEvents.ON_WINDOW_CLOSE, new EventListener<Event>() { directoryDialog.addEventListener(DialogEvents.ON_WINDOW_CLOSE, new EventListener<Event>() {
@Override @Override
public void onEvent(Event event) throws Exception { public void onEvent(Event event) throws Exception {
callImportProcess(directoryDialog.getPath()); callImportProcess(directoryDialog.getPath());
} }
});
} // onEvent
private void callImportProcess(String path) { private void callImportProcess(String directory) {
String directory = directoryDialog.getPath();
if(directory == null) return; if(directory == null) return;
// //
statusBar.setStatusLine(directory); statusBar.setStatusLine(directory);
@ -256,10 +300,111 @@ public class WTranslationDialog extends TranslationController implements IFormCo
statusBar.setStatusLine(msg.toString()); statusBar.setStatusLine(msg.toString());
} }
} }
});
} // actionPerformed
public ADForm getForm() { private void processUploadMedia(Media media) throws AdempiereException {
return form; if (media == null)
return;
InputStream istream;
if (media.isBinary()) {
istream = media.getStreamData();
} else {
istream = new ReaderInputStream(media.getReaderData());
} }
if (log.isLoggable(Level.CONFIG)) log.config(media.getName());
unZipAndProcess(istream);
}
private void unZipAndProcess(InputStream istream) throws AdempiereException {
FileOutputStream ostream = null;
File file = null;
try {
file = File.createTempFile("trlImport", ".zip");
ostream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int read;
while ((read = istream.read(buffer)) != -1) {
ostream.write(buffer, 0, read);
}
} catch (Throwable e) {
throw new AdempiereException("Copy zip failed", e);
} finally {
if (ostream != null) {
try {
ostream.close();
} catch (Exception e2) {}
}
}
// create temp folder
File tempfolder;
try {
tempfolder = File.createTempFile(m_AD_Language.getValue(), ".trl");
tempfolder.delete();
tempfolder.mkdir();
} catch (IOException e1) {
throw new AdempiereException("Problem creating temp folder", e1);
}
String suffix = "_" + m_AD_Language.getValue() + ".xml";
ZipFile zipFile = null;
boolean validfile = false;
try {
zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();
if (entry.isDirectory()) {
// ignore folders
log.warning("Imported zip must not contain folders, ignored folder" + entry.getName());
continue;
}
if (! entry.getName().endsWith(suffix)) {
// not valid file
log.warning("Ignored file " + entry.getName());
continue;
}
if (log.isLoggable(Level.INFO)) log.info("Extracting file: " + entry.getName());
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(tempfolder.getPath() + File.separator + entry.getName())));
validfile = true;
}
} catch (Throwable e) {
throw new AdempiereException("Uncompress zip failed", e);
} finally {
if (zipFile != null)
try {
zipFile.close();
} catch (IOException e) {}
}
if (!validfile) {
throw new AdempiereException("ZIP file invalid, doesn't contain *" + suffix + " files");
}
callImportProcess(tempfolder.getPath());
file.delete();
try {
FileUtils.deleteDirectory(tempfolder);
} catch (IOException e) {}
}
public static final void copyInputStream(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
} }