1 /*
2  * ************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org.  If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  * ***********************************************************************
28  */
29 
30 package graphical;
31 
32 import java.util.ArrayList;
33 
34 /**
35  *
36  * @author ll93751
37  */
38 public class Office implements IOffice
39 {
40     private ParameterHelper m_aParameterHelper;
41     private String m_sDocumentName;
42     private String m_sResult;
43     private IOffice m_aOffice = null;
44 
45     public Office(ParameterHelper _aParam, String _sResult)
46     {
47         m_aParameterHelper = _aParam;
48         m_sResult = _sResult;
49 
50         if (_aParam.getReferenceType().toLowerCase().equals("ooo") ||
51             _aParam.getReferenceType().toLowerCase().equals("o3") ||
52             _aParam.getReferenceType().toLowerCase().equals("ps") ||
53             _aParam.getReferenceType().toLowerCase().equals("pdf"))
54         {
55             m_aOffice = new OpenOfficePostscriptCreator(_aParam, m_sResult);
56         }
57         else if (_aParam.getReferenceType().toLowerCase().equals("msoffice"))
58         {
59             m_aOffice = new MSOfficePostscriptCreator(_aParam, m_sResult);
60         }
61     }
62 
63 
64     /**
65      * Load a document with an already started Office.
66      * @param _sDocumentName
67      * @throws graphical.OfficeException
68      */
69     public void load(String _sDocumentName) throws OfficeException
70     {
71         m_sDocumentName = _sDocumentName;
72         // check if given file is a picture, then do nothing
73         String sDocumentSuffix = FileHelper.getSuffix(m_sDocumentName);
74         if (sDocumentSuffix.toLowerCase().endsWith(".png") ||
75             sDocumentSuffix.toLowerCase().endsWith(".gif") ||
76             sDocumentSuffix.toLowerCase().endsWith(".jpg") ||
77             sDocumentSuffix.toLowerCase().endsWith(".bmp"))
78         {
79             throw new OfficeException("The given document is not a document type.");
80         }
81 
82         // TODO: we should start the office after we know if we really need an Office.
83         if (m_aOffice != null)
84         {
85             if (sDocumentSuffix.toLowerCase().endsWith(".odb"))
86             {
87                 if (m_aParameterHelper.getReferenceType().toLowerCase().equals("msoffice"))
88                 {
89                     // we can't handle .odb with msoffice
90                     return;
91                 }
92                 // TODO: run through all documents which exists as reports in odb files
93                 OpenOfficeDatabaseReportExtractor aExtractor = new OpenOfficeDatabaseReportExtractor(m_aParameterHelper);
94                 ArrayList aList = aExtractor.load(m_sDocumentName);
95                 if (aList != null)
96                 {
97                     // remove the whole section about the 'name'.odb there are no information we need
98                     // we will create a new one.
99                     String sIniFile = FileHelper.appendPath(m_sResult, "index.ini");
100                     IniFile aIniFile2 = new IniFile(sIniFile);
101                     String sSection = FileHelper.getBasename(_sDocumentName); // name of the odb file
102                     aIniFile2.removeSection(sSection);
103                     aIniFile2.close();
104 
105                     for (int i=0; i<aList.size();i++)
106                     {
107                         String sDocumentName = (String)aList.get(i);
108                         m_aOffice.load(sDocumentName);
109                         m_aOffice.storeAsPostscript();
110 
111 
112                         // foreach Report found in the .odb file, create an entry 'report'<number> in the original <name>.odb Section
113                         // so it is possible to run through all reports by the given .odb name
114                         IniFile aIniFile = new IniFile(sIniFile);
115                         // String sSection = FileHelper.getBasename(_sDocumentName); // name of the odb file
116                         int nFileCount = aIniFile.getIntValue(sSection, "reportcount", 0);
117                         String sValue = FileHelper.getBasename(sDocumentName);    // name of the corresponding report
118                         aIniFile.insertValue(sSection, "report" + nFileCount, sValue);
119                         aIniFile.insertValue(sSection, "reportcount", nFileCount + 1);
120                         aIniFile.close();
121                     }
122                 }
123                 else
124                 {
125                     throw new OfficeException("Can't open the document " + m_sDocumentName);
126                 }
127             }
128             else
129             {
130                 m_aOffice.load(_sDocumentName);
131             }
132         }
133     }
134 
135     public void storeAsPostscript() throws OfficeException
136     {
137         if (m_aOffice != null)
138         {
139             if (m_sDocumentName.endsWith(".odb"))
140             {
141                 // this has already be done by load() for odb files.
142             }
143             else
144             {
145                 m_aOffice.storeAsPostscript();
146             }
147 
148 //          FileHelper.addBasenameToIndex(sOutputFilename);
149         }
150     }
151 
152     public void start() throws OfficeException
153     {
154         if (m_aOffice != null)
155         {
156             m_aOffice.start();
157         }
158     }
159 
160     public void close() throws OfficeException
161     {
162         if (m_aOffice != null)
163         {
164             m_aOffice.close();
165         }
166     }
167 
168 
169 
170 
171 }
172