001    /*
002     * 
003     * $Revision: 1.12 $ $Date: 2009/03/23 10:03:47 $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.frontend.metsmods;
025    
026    
027    import java.applet.AppletContext;
028    import java.awt.event.ActionEvent;
029    import java.awt.event.ActionListener;
030    import java.io.IOException;
031    import java.io.InputStream;
032    import java.net.MalformedURLException;
033    import java.net.URL;
034    import java.util.ArrayList;
035    import java.util.Locale;
036    import java.util.ResourceBundle;
037    
038    import javax.swing.JApplet;
039    import javax.swing.JButton;
040    import javax.swing.JLabel;
041    import javax.swing.JOptionPane;
042    import javax.swing.JPanel;
043    import javax.swing.JScrollPane;
044    
045    import org.jdom.Document;
046    import org.jdom.JDOMException;
047    import org.jdom.input.SAXBuilder;
048    import org.jdom.output.XMLOutputter;
049    import org.mycore.frontend.fileupload.MCRMetsModsCommunicator;
050    
051    /**
052     * This applet allows to modify a mets file.
053     * 
054     * @author Stefan Freitag (sasf)
055     * 
056     * @version $Revision: 1.12 $ $Date: 2009/03/23 10:03:47 $
057     */
058    
059    public class MCRMetsModsApplet extends JApplet{
060            
061            MCRMetsModsPictureContainer mmpc;
062            JButton save_btn = new JButton("Save");
063            JButton cancel_btn = new JButton("Cancel");
064            
065            JScrollPane scroller = new JScrollPane();
066    
067            ArrayList<MCRMetsModsPicture> pictures;
068            
069        protected String uploadId;
070        protected String peerURL;
071        protected String targetURL;
072                    
073            public void init()
074            {
075                    JPanel panel = new JPanel();
076                    setContentPane(panel);
077                    
078                    uploadId = this.getParameter("uploadId");
079                    String httpSession = this.getParameter("httpSession");
080            peerURL = addSessionInfo(this.getParameter("ServletsBase") + "MCRUploadServlet", httpSession);
081                    final String mets_url = this.getParameter("metsfile");
082                    if(mets_url==null) 
083                    {
084                            JOptionPane.showMessageDialog(this, "Sorry, there was no valid mets.xml file found!");
085                            return;
086                    }
087                    targetURL = this.getParameter("url");
088                    
089                    if(this.getParameter("language").compareTo("id")==0)
090                            this.setLocale(new Locale("in"));
091                    else
092                            this.setLocale(new Locale(this.getParameter("language")));
093                    
094                    panel.setLayout(null);
095                                                                    
096                    save_btn.setBounds(10,370,100,20);
097                    cancel_btn.setBounds(120,370,100,20);
098                    
099                    //((JPanel)this.getGlassPane()).add(sepa);
100                    //this.getGlassPane().setVisible(true);
101                    
102                            JPanel jp_t = new JPanel();
103                            jp_t.setBackground(java.awt.Color.orange);
104                            jp_t.setLayout(null);
105                            JLabel title_label1 = new JLabel(translateI18N("MCRMetsmodsApplet.title.filename"));
106                            JLabel title_label2 = new JLabel(translateI18N("MCRMetsmodsApplet.title.index"));
107                            JLabel title_label3 = new JLabel(translateI18N("MCRMetsmodsApplet.title.orderlabel"));
108                            title_label1.setBounds(10,2,100,20);
109                            title_label2.setBounds(120,2,100,20);
110                            title_label3.setBounds(230,2,100,20);
111                            
112                            jp_t.add(title_label1);
113                            jp_t.add(title_label2);
114                            jp_t.add(title_label3);
115                    
116                            ((JPanel)this.getGlassPane()).setLayout(null);
117                            jp_t.setBounds(15,15,this.getWidth()-45,20);
118                            ((JPanel)this.getGlassPane()).add(jp_t);
119                            this.getGlassPane().setVisible(true);
120                    
121                    pictures = openMETS(mets_url);
122                    
123                    mmpc = new MCRMetsModsPictureContainer();
124                    
125                    scroller = new JScrollPane(mmpc);
126                    scroller.setBounds(10,10,this.getWidth()-20,this.getHeight()-50);
127                                    
128                    int k=0;
129                    for(int i=0;i<pictures.size();i++)
130                            if(((MCRMetsModsPicture)pictures.get(i)).getPicture().length()*10>k) k=((MCRMetsModsPicture)pictures.get(i)).getPicture().length()*10;
131                    
132                            title_label2.setBounds(k+10,2,100,20);  //adapting text length of filename for position of label
133                            title_label3.setBounds(k+130,2,100,20); //adapting text length of filename for position of label
134                    
135                    for(int i=0;i<pictures.size();i++)
136                    {
137                            MCRMetsModsPicture mmpic = (MCRMetsModsPicture)pictures.get(i);
138                            MCRMetsModsPictureItem mmpi = new MCRMetsModsPictureItem(mmpic,k);
139                            mmpc.addItem(mmpi);
140                    }
141                            
142                    mmpc.setPreferredSize(mmpc.getSize());
143                    
144                    save_btn.addActionListener(new ActionListener(){
145                            public void actionPerformed(ActionEvent e)
146                            {
147                                    pictures = new ArrayList<MCRMetsModsPicture>();
148                                    //pictures = processChangedList();
149                                    for(int i=0;i<mmpc.itemlist.size();i++)
150                                            pictures.add(mmpc.itemlist.get(i).getMetsModsPicture());
151                                    
152                                    final String mets = closeMETS(mets_url,pictures);
153                                    
154                                    if(mets==null) return;
155                                    
156                                    Thread th = new Thread() {
157                                public void run() {
158                                    MCRMetsModsCommunicator comm = new MCRMetsModsCommunicator(peerURL, uploadId);
159                                    comm.uploadMets(mets);
160                            
161                                    returnToURL();
162                                }
163                            };
164                            th.start();
165                            
166                                    
167                            }
168                    });
169                    
170                    cancel_btn.addActionListener(new ActionListener(){
171                            public void actionPerformed(ActionEvent e)
172                            {
173                                    returnToURL();
174                            }
175                    });
176                    
177                    save_btn.setText(translateI18N("MCRMetsmodsApplet.save"));
178                    cancel_btn.setText(translateI18N("MCRMetsmodsApplet.cancel"));
179                    
180                    panel.add(scroller);
181                    panel.add(save_btn);
182                    panel.add(cancel_btn);
183                    
184            }
185            
186            private void returnToURL()
187            {
188                    try{
189                    URL url = new URL(targetURL);
190                    AppletContext context = getAppletContext();
191                context.showDocument(url);
192            }catch(Exception e)
193            {
194                    e.printStackTrace();
195            }
196            }
197            
198            
199            private void showPictureList(ArrayList picturelist)
200            {
201                    System.out.println("-------------------------------------------------------");
202                    for(int i=0;i<picturelist.size();i++)
203                    {
204                            MCRMetsModsPicture mmpic = (MCRMetsModsPicture)picturelist.get(i);
205                            mmpic.show();
206                    }
207            }
208            
209                    
210            private String closeMETS(String mets_url, ArrayList picturelist)
211            {
212                    try {
213                            URL url = new URL(mets_url);
214                            InputStream di = url.openConnection().getInputStream();
215                            Document doc = new SAXBuilder().build(di);
216                            di.close();
217                            /*MetsModsUtil mmu = new MetsModsUtil();
218                            ArrayList piclist = mmu.getFileList(doc);*/
219                            doc = MCRMetsModsUtil.getMetsFile(picturelist, doc);
220                            
221                            XMLOutputter xmlout = new XMLOutputter();
222                            //xmlout.output(doc, System.out);
223                            return xmlout.outputString(doc);
224                            
225                    } catch (MalformedURLException e) {
226                            // TODO Auto-generated catch block
227                            e.printStackTrace();
228                    } catch (IOException e) {
229                            // TODO Auto-generated catch block
230                            e.printStackTrace();
231                    } catch (JDOMException e) {
232                            // TODO Auto-generated catch block
233                            e.printStackTrace();
234                    }
235                    
236                    return null;
237                    
238            }
239            
240            private ArrayList openMETS(String mets_url)
241            {
242                    try {
243                            URL url = new URL(mets_url);
244                            InputStream di = url.openConnection().getInputStream();
245                            Document doc = new SAXBuilder().build(di);
246                            di.close();
247                            /*MetsModsUtil mmu = new MetsModsUtil();
248                            ArrayList piclist = mmu.getFileList(doc);*/
249                            ArrayList piclist = MCRMetsModsUtil.getFileList(doc);
250                            /*for(int i=0;i<piclist.size();i++)
251                                    System.out.println(((MCRMetsModsPicture)piclist.get(i)).getPicture()+":"+((MCRMetsModsPicture)piclist.get(i)).getOrder());*/
252                            return piclist;
253                    } catch (MalformedURLException e) {
254                            // TODO Auto-generated catch block
255                            e.printStackTrace();
256                    } catch (IOException e) {
257                            // TODO Auto-generated catch block
258                            e.printStackTrace();
259                    } catch (JDOMException e) {
260                            // TODO Auto-generated catch block
261                            e.printStackTrace();
262                    }
263                    return null;
264            }
265            
266            private String addSessionInfo(String url, String sessionId) {
267    
268            if ((url == null) || (sessionId == null)) {
269                return url;
270            }
271            String path = url;
272            String query = "";
273            int queryPos = url.indexOf('?');
274            if (queryPos >= 0) {
275                path = url.substring(0, queryPos);
276                query = url.substring(queryPos);
277            }
278            StringBuffer sb = new StringBuffer(path);
279            sb.append(";jsessionid=");
280            sb.append(sessionId);
281            sb.append(query);
282            return sb.toString();
283        }
284            
285            private final String translateI18N(String label) {
286            String result;
287            Locale currentLocale = getLocale();
288            System.out.println(currentLocale);
289            try {
290                ResourceBundle message = ResourceBundle.getBundle("messages", currentLocale);
291                result = message.getString(label);
292            } catch (java.util.MissingResourceException mre) {
293                result = "???" + label + "???";
294                System.err.println(mre.getMessage());
295            }
296    
297            return result;
298        }
299    }
300    
301