Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * Wikitty :: api
   * %%
   * Copyright (C) 2009 - 2010 CodeLutin, Benjamin Poussin
   * %%
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as 
   * published by the Free Software Foundation, either version 3 of the 
  * License, or (at your option) any later version.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Lesser Public License for more details.
  * 
  * You should have received a copy of the GNU General Lesser Public 
  * License along with this program.  If not, see
  * <http://www.gnu.org/licenses/lgpl-3.0.html>.
  * #L%
  */
 package org.nuiton.wikitty.addons;
 
 import java.io.File;
 import java.io.Reader;
 import java.io.Writer;
 import java.net.URL;
 import java.util.Map;
 import java.util.UUID;
 
Import/export service.

Author(s):
poussin
Version:
$Revision$ Last update: $Date$ by : $Author$
 
 public class WikittyImportExportService {
 
     static public enum FORMAT {
         XML(new ImportExportXML()),
         CSV(new ImportExportCSV());

        
ieport == Importer/Exporter
 
         protected ImportExportMethod ieporter;
         FORMAT(ImportExportMethod ieporter) {
             this. = ieporter;
         }
 
         public ImportExportMethod ieporter() {
             return ;
         }
     };
 
     WikittyClient client;
    
directory path where export asynchronous file are stored
 
     protected String exportDirectory = "/tmp/";
    
url used by client to retrieve export file when job is ended
 
     protected String exportPublicURL = "file:///tmp/";

    
Executor that do import export task
 
     protected ExecutorService importExportExecutor;

    
contains all import or export task, key is job id send to client
 
     protected Map<StringFuture<String>> importExportTask =
             new HashMap<StringFuture<String>>();
 
 
     public WikittyImportExportService(WikittyClient client) {
         this. = client;
        ApplicationConfig config = client.getConfig();
         = config.getOption(
                ..getKey());
         = config.getOption(
                ..getKey());
        int maxThread = config.getOptionAsInt(
                ..getKey());
        this. =
                Executors.newFixedThreadPool(maxThread);
    }

    

Deprecated:
since 3.2 use
Parameters:
config
securityToken
ws
    @Deprecated
            ApplicationConfig configString securityTokenWikittyService ws) {
        this(new WikittyClient(configwssecurityToken));
    }
    public WikittyClient getClient() {
        return ;
    }
    public WikittyService getWikittyService() {
        return getClient().getWikittyService();
    }
    public String getExportDirectory() {
        return ;
    }
    public String getExportPublicURL() {
        return ;
    }
    public void syncImport(FORMAT formatString s) {
        Reader reader = new StringReader(s);
        syncImport(formatreader);
    }
    public void syncImport(FORMAT formatReader reader) {
        ImportTask task = new ImportTask(formatreader);
        task.run();
    }
    public void syncImportFromUri(FORMAT formatString uri) {
        try {
            URL url = new URL(uri);
            Reader reader = new InputStreamReader(url.openStream());
            ImportTask task = new ImportTask(formatreader);
            task.run();
        } catch (Exception eee) {
            throw new WikittyException(String.format(
                    "Can't import in format %s uri %s"formaturi), eee);
        }
    }
    public String asyncImportFromUri(FORMAT formatString uri) {
        try {
            URL url = new URL(uri);
            Reader reader = new InputStreamReader(url.openStream());
            ImportTask task = new ImportTask(formatreader);
            FutureTask<Stringfuture = new FutureTask<String>(tasknull);
            .submit(future);
            String jobId = UUID.randomUUID().toString();
            .put(jobIdfuture);
            return jobId;
        } catch (Exception eee) {
            throw new WikittyException(String.format(
                    "Can't import in format %s uri %s"formaturi), eee);
        }
    }

    
Asynchronous export by example.

Parameters:
format export format
e sample
Returns:
job id
        WikittyQuery query = new WikittyQueryMaker().wikitty(e).end();
        String result = asyncExportAllByQuery(formatquery);
        return result;
    }

    
Synchronous export by example.

Parameters:
format export format
e sample
Returns:
export string
    public String syncExportAllByExample(FORMAT formatBusinessEntityImpl e) {
        WikittyQuery query = new WikittyQueryMaker().wikitty(e).end();
        String result = syncExportAllByQuery(formatquery);
        return result;
    }

    
Synchronous export by example.

Parameters:
format export format
e sample
Returns:
export string
    public void syncExportAllByExample(FORMAT formatBusinessEntityImpl e,
                                         Writer writer) {
        WikittyQuery query = new WikittyQueryMaker().wikitty(e).end();
        syncExportAllByQuery(formatquerywriter);
    }

    
Asynchronous export by criteria.

Parameters:
format export format
query criteria
Returns:
export as string
    public String asyncExportAllByQuery(FORMAT formatWikittyQuery query) {
        try {
            String jobId = UUID.randomUUID().toString();
            File file = new File(jobId);
            file.getParentFile().mkdirs();
            String url =  + jobId;
            Writer result = new FileWriter(file);
            ExportQueryTask task = new ExportQueryTask(formatqueryresult);
            FutureTask<Stringfuture = new FutureTask<String>(taskurl);
            .submit(future);
            .put(jobIdfuture);
            return jobId;
        } catch (Exception eee) {
            throw new WikittyException(String.format(
                    "Can't export in format %s"format), eee);
        }
    }

    
Synchronous export by criteria.

Parameters:
format export format
query criteria
Returns:
export as string
    public String syncExportAllByQuery(FORMAT formatWikittyQuery query) {
        StringWriter result = new StringWriter();
        syncExportAllByQuery(formatqueryresult);
        return result.toString();
    }

    
Synchronous export by criteria.

Parameters:
format export format
query criteria
Returns:
export as string
    public void syncExportAllByQuery(FORMAT formatWikittyQuery queryWriter writer) {
        ExportQueryTask task = new ExportQueryTask(formatquerywriter);
        task.run();
    }

    
Asynchronous export by criteria.

Deprecated:
since 3.4 user
Parameters:
format export format
criteria criteria
Returns:
export as string
    @Deprecated
    public String asyncExportAllByCriteria(FORMAT formatCriteria criteria) {
        try {
            String jobId = UUID.randomUUID().toString();
            File file = new File(jobId);
            file.getParentFile().mkdirs();
            String url =  + jobId;
            Writer result = new FileWriter(file);
            ExportTask task = new ExportTask(.getSecurityToken(),
                    .getWikittyService(), formatcriteriaresult);
            FutureTask<Stringfuture = new FutureTask<String>(taskurl);
            .submit(future);
            .put(jobIdfuture);
            return jobId;
        } catch (Exception eee) {
            throw new WikittyException(String.format(
                    "Can't export in format %s"format), eee);
        }
    }

    
Synchronous export by criteria.

Deprecated:
since 3.4 user
Parameters:
format export format
criteria criteria
Returns:
export as string
    @Deprecated
    public String syncExportAllByCriteria(FORMAT formatCriteria criteria) {
        StringWriter result = new StringWriter();
        syncExportAllByCriteria(formatcriteriaresult);
        return result.toString();
    }

    
Synchronous export by criteria.

Deprecated:
since 3.4 user
Parameters:
format export format
criteria criteria
Returns:
export as string
    @Deprecated
    public void syncExportAllByCriteria(FORMAT formatCriteria criteriaWriter writer) {
        ExportTask task = new ExportTask(.getSecurityToken(),
                    .getWikittyService(), formatcriteriawriter);
        task.run();
    }

    
Return job information.

Parameters:
jobId job id
Returns:
job state
    public JobState infoJob(String jobId) {
        try {
            Future<Stringfuture = .get(jobId);
            JobState result = new JobState();
            if (future.isDone()) {
                result.status = "done";
                result.resourceUri = future.get();
            } else if (future.isCancelled()) {
                result.status = "cancelled";
            } else {
                result.status = "inProgress";
            }
            return result;
        } catch (Exception eee) {
            throw new WikittyException(String.format(
                    "Can't retrieve job info for job %s"jobId), eee);
        }
    }
    public void cancelJob(String jobId) {
        Future<Stringfuture = .get(jobId);
        future.cancel(true); // true to kill process, perhaps to strong ?
    }
    public void freeJobResource(String jobId) {
        Future<Stringfuture = .remove(jobId);
        if (future != null) {
            File file = new File(jobId);
            file.delete();
        }
    }
New to GrepCode? Check out our FAQ X