001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: tao $'
006 * '$Date: 2012-06-07 20:57:40 +0000 (Thu, 07 Jun 2012) $' 
007 * '$Revision: 29891 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.kar;
031
032import java.io.File;
033import java.io.IOException;
034
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.ecoinformatics.ecogrid.client.IdentifierServiceClient;
038import org.kepler.objectmanager.lsid.KeplerLSID;
039import org.kepler.objectmanager.lsid.LSIDGenerator;
040import org.kepler.objectmanager.repository.Repository;
041
042/**
043 * This action imports a kar file to the system
044 * 
045 *@author Chad Berkley, Aaron Schultz
046 *@created 10/11/2006
047 */
048public class UploadToRepository {
049
050        private static final Log log = LogFactory.getLog(UploadToRepository.class);
051        private static final boolean isDebugging = log.isDebugEnabled();
052
053        /**
054         * The repository that we are uploading to.
055         */
056        private Repository _repository;
057
058        /**
059         * The KARFile that we are uploading.
060         */
061        private KARFile _karFile;
062
063        /**
064         * Whether or not this KAR is public.
065         */
066        private boolean publicFile = false;
067
068        /**
069         * The session id to use with the webservice.
070         */
071        private String _sessionId;
072
073        // Accessor methods
074
075        public String getSessionId() {
076                return _sessionId;
077        }
078
079        public void setSessionId(String sessionId) {
080                _sessionId = sessionId;
081        }
082
083        public boolean isPublicFile() {
084                return publicFile;
085        }
086
087        public void setPublicFile(boolean isPublic) {
088                this.publicFile = isPublic;
089        }
090
091        /**
092         * 
093         * @return Repository where we are uploading the file to
094         */
095        public Repository getRepository() {
096                return _repository;
097        }
098
099        /**
100         * Set the Repository to upload the KARFile to.
101         * 
102         * @param repository
103         */
104        public void setRepository(Repository repository) {
105                this._repository = repository;
106        }
107
108        /**
109         * Constructor
110         * 
111         * @param parent
112         *            the "frame" (derived from ptolemy.gui.Top) where the menu is
113         *            being added.
114         */
115        public UploadToRepository(File karFile) {
116
117                try {
118                        _karFile = new KARFile(karFile);
119                } catch (IOException e) {
120                        e.printStackTrace();
121                }
122
123        }
124
125        public boolean isAlreadyRegistered(KeplerLSID lsid) throws Exception {
126                IdentifierServiceClient identificationClient = new IdentifierServiceClient(
127                                _repository.getLSIDServerURL());
128                return identificationClient.isRegistered(lsid.toString());
129        }
130
131        /**
132         * Use the given NamedObj to generate the metadata for metacat.
133         * 
134         * @param object
135         * */
136        public boolean uploadMetadata(String metadata) {
137
138                try {
139
140                        LSIDGenerator lsidGen = LSIDGenerator.getInstance();
141                        KeplerLSID metadataLSID = lsidGen.getNewLSID();
142
143                        // check if it exists
144                        if (isAlreadyRegistered(metadataLSID)) {
145                                if (isDebugging)
146                                        log.debug("metadata already exists for this lsid: "
147                                                        + metadataLSID);
148                                return false;
149                        }
150
151                        if (isDebugging)
152                                log.debug("uploading actor metadata with id " + metadataLSID
153                                                + " using sessionid " + getSessionId());
154                        _repository.put(metadata, metadataLSID, getSessionId());
155                        if (isDebugging)
156                                log.debug("uploaded actor metadata with id " + metadataLSID);
157
158                        // create an access file so we can make this entity public on
159                        // the ecogrid
160                        // Why are we using a new LSID here? -aaron
161                        KeplerLSID accessLSID = LSIDGenerator.getInstance().getNewLSID();
162
163                        // put the access file for the metadata
164                        if (isDebugging)
165                                log.debug("uploading access file for AM with id " + accessLSID);
166                        String accessDoc = buildAccessDocument(metadataLSID, isPublicFile());
167                        _repository.put(accessDoc, accessLSID, getSessionId());
168                        if (isDebugging)
169                                log.debug("uploaded access file for AM with id " + accessLSID);
170
171                        // register an id for the access file for the kar
172                        KeplerLSID karAccessLSID = lsidGen.getNewLSID();
173
174                        // put the access file for the kar file
175                        if (isDebugging)
176                                log.debug("uploading access file for kar with access id "
177                                                + karAccessLSID);
178
179                        String karAccessDoc = buildAccessDocument(_karFile.getLSID(),
180                                        isPublicFile());
181                        _repository.put(karAccessDoc, karAccessLSID, getSessionId());
182                        if (isDebugging)
183                                log.debug("uploaded access file for kar with id "
184                                                + karAccessLSID);
185
186                } catch (Exception e) {
187                        e.printStackTrace();
188                        return false;
189                }
190                return true;
191
192        }
193
194        /**
195         * uploads a single object to the repository
196         * 
197         * @return true if the upload was succesful
198         */
199        public boolean uploadFile() throws Exception {
200                if (isDebugging)
201                        log.debug("uploadFile()");
202
203                KeplerLSID karLSID = _karFile.getLSID();
204
205                if (isAlreadyRegistered(karLSID)) {
206                  log.warn("The kar file has already been registered and can't be uploaded again");
207                        return false;
208                }
209
210                if (isDebugging)
211                        log.debug("uploading kar file with id " + karLSID);
212
213                _repository.put(_karFile.getFileLocation(), karLSID, getSessionId());
214                if (isDebugging)
215                        log.debug("uploaded kar file with id " + karLSID);
216
217                return true;
218        }
219
220        /**
221         * uploads a document as the kepler user with public read permissions
222         */
223        public static String buildAccessDocument(KeplerLSID lsid) {
224                return buildAccessDocument(lsid, null, true);
225        }
226
227        public static String buildAccessDocument(KeplerLSID lsid, boolean publicDoc) {
228                return buildAccessDocument(lsid, null, publicDoc);
229        }
230
231        /**
232         * build an access document that gives 'public' read access to the inserted
233         * documents.
234         */
235        public static String buildAccessDocument(KeplerLSID lsid, String owner,
236                        boolean publicDoc) {
237                StringBuffer sb = new StringBuffer();
238                sb.append("<?xml version=\"1.0\"?>\n");
239                sb
240                                .append("<eml:eml packageId=\"\" system=\"knb\" "
241                                                + "xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\" "
242                                                + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
243                                                + "xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\">\n");
244                sb.append("<dataset>\n");
245                sb.append("<title>access for " + lsid.toString() + "</title>\n");
246                sb.append("<creator id=\"1\">\n");
247                sb.append("  <individualName><surName>kepler</surName>\n");
248                sb.append("  </individualName>\n");
249                sb.append("</creator>\n");
250
251                sb.append("<contact><references>1</references>\n");
252                sb.append("</contact>\n");
253
254                if (publicDoc) {
255                        sb.append("<access authSystem=\"knb\" order=\"allowFirst\">\n");
256                        sb.append("  <allow>\n");
257                        sb.append("    <principal>public</principal>\n");
258                        sb.append("    <permission>read</permission>\n");
259                        sb.append("  </allow>\n");
260                        sb.append("</access>\n");
261                }
262
263                if (owner != null) {
264                        sb.append("<access authSystem=\"knb\" order=\"allowFirst\">\n");
265                        sb.append("  <allow>\n");
266                        sb.append("    <principal>" + owner + "</principal>\n");
267                        sb.append("    <permission>read</permission>\n");
268                        sb.append("    <permission>write</permission>\n");
269                        sb.append("  </allow>\n");
270                        sb.append("</access>\n");
271                }
272
273                sb.append("<dataTable id=\"x\">\n");
274
275                sb.append("<entityName>asdf</entityName>\n");
276
277                sb.append("<physical>\n");
278                sb.append("  <objectName>tmp</objectName>\n");
279                sb.append("  <dataFormat> \n");
280                sb.append("  <externallyDefinedFormat>\n");
281                sb.append("    <formatName>application/vnd.ms-excel</formatName>\n");
282                sb.append("  </externallyDefinedFormat>\n");
283                sb.append("  </dataFormat>\n");
284
285                sb.append("  <distribution>\n");
286                sb.append("    <online>\n");
287                sb.append("      <url>ecogrid://knb/" + lsid.getNamespace() + "."
288                                + lsid.getObject() + "." + lsid.getRevision() + "</url>\n");
289                sb.append("    </online>\n");
290                sb.append("   </distribution>\n");
291                sb.append("</physical>\n");
292
293                sb.append("<attributeList>\n");
294                sb.append("  <attribute id=\"2\">\n");
295                sb.append("    <attributeName>0</attributeName>\n");
296                sb.append("    <attributeDefinition>0</attributeDefinition>\n");
297                sb.append("    <measurementScale>\n");
298                sb.append("      <interval>\n");
299                sb.append("        <unit>\n");
300                sb
301                                .append("          <standardUnit>metersPerSecondSquared</standardUnit>\n");
302                sb.append("        </unit>\n");
303                sb.append("        <precision>.2</precision>\n");
304                sb.append("        <numericDomain>\n");
305                sb.append("          <numberType>natural</numberType>\n");
306                sb.append("        </numericDomain>\n");
307                sb.append("      </interval>\n");
308                sb.append("    </measurementScale>\n");
309                sb.append("  </attribute>\n");
310                sb.append("</attributeList>\n");
311
312                sb.append("</dataTable>\n");
313                sb.append("</dataset>\n");
314                sb.append("</eml:eml>\n");
315
316                return sb.toString();
317        }
318
319}