Search in sources :

Example 1 with ArchiveExportOption

use of com.twinsoft.convertigo.engine.enums.ArchiveExportOption in project convertigo by convertigo.

the class CarUtils method makeArchive.

public static File makeArchive(File file, Project project, Set<ArchiveExportOption> archiveExportOptions) throws EngineException {
    Set<File> undeployedFiles = getUndeployedFiles(project.getName());
    String projectName = project.getName();
    String dirPath = project.getDirPath();
    File projectDir = new File(dirPath);
    File skipTestCase = null;
    try {
        if (!archiveExportOptions.contains(ArchiveExportOption.includeTestCase)) {
            skipTestCase = new File(projectDir, "_private/noTestCase/c8oProject.yaml");
            CarUtils.exportProject(project, skipTestCase.getAbsolutePath(), false);
        }
        for (ArchiveExportOption opt : ArchiveExportOption.values()) {
            if (!archiveExportOptions.contains(opt)) {
                for (File dir : opt.dirs(projectDir)) {
                    undeployedFiles.add(dir);
                }
            }
        }
        FileUtils.deleteQuietly(file);
        File f = ZipUtils.makeZip(file.getAbsolutePath(), dirPath, projectName, undeployedFiles);
        if (skipTestCase != null) {
            Map<String, String> zip_properties = new HashMap<>();
            zip_properties.put("create", "false");
            zip_properties.put("encoding", "UTF-8");
            URI zip_disk = URI.create("jar:" + f.toURI());
            try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk, zip_properties)) {
                Path addNewFile = skipTestCase.getParentFile().toPath();
                Files.walk(addNewFile).forEach(p -> {
                    try {
                        String relat = addNewFile.relativize(p).toString();
                        if (!relat.isEmpty()) {
                            Files.copy(p, zipfs.getPath(projectName, relat));
                        }
                    } catch (IOException e) {
                        Engine.logEngine.debug("(CarUtils) failed to copy project without TestCase: " + e);
                    }
                });
            }
        }
        return f;
    } catch (Exception e) {
        throw new EngineException("Unable to make the archive file for the project \"" + projectName + "\".", e);
    } finally {
        if (skipTestCase != null) {
            FileUtils.deleteQuietly(skipTestCase.getParentFile());
        }
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileSystem(java.nio.file.FileSystem) EngineException(com.twinsoft.convertigo.engine.EngineException) ArchiveExportOption(com.twinsoft.convertigo.engine.enums.ArchiveExportOption) IOException(java.io.IOException) File(java.io.File) URI(java.net.URI) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 2 with ArchiveExportOption

use of com.twinsoft.convertigo.engine.enums.ArchiveExportOption in project convertigo by convertigo.

the class Export method writeResponseResult.

@Override
protected void writeResponseResult(HttpServletRequest request, HttpServletResponse response) throws IOException, EngineException {
    String projectName = request.getParameter("projectName");
    String exportOptionsTxt = request.getParameter("exportOptions");
    HeaderName.ContentDisposition.setHeader(response, "attachment; filename=\"" + projectName + ".car\"");
    response.setContentType(MimeType.Zip.value());
    // if any, backup existing CAR file
    File f = new File(Engine.projectDir(projectName) + ".car");
    long lastDate = -1;
    if (f.exists()) {
        lastDate = f.lastModified();
        File oldFile = new File(Engine.projectDir(projectName) + ".car.old");
        FileUtils.deleteQuietly(oldFile);
        f.renameTo(oldFile);
    }
    if (!Engine.theApp.databaseObjectsManager.existsProject(projectName)) {
        throw new IllegalArgumentException("The project '" + projectName + "' does not exist!");
    }
    Set<ArchiveExportOption> exportOptions = ArchiveExportOption.all;
    if (exportOptionsTxt != null) {
        try {
            JSONObject exportOptionsJson = new JSONObject(exportOptionsTxt);
            exportOptions = new HashSet<ArchiveExportOption>(exportOptions);
            for (Iterator<?> i = exportOptionsJson.keys(); i.hasNext(); ) {
                String key = (String) i.next();
                try {
                    ArchiveExportOption opt = ArchiveExportOption.valueOf(key);
                    if (!exportOptionsJson.getBoolean(key)) {
                        exportOptions.remove(opt);
                    }
                } catch (Exception e) {
                    Engine.logAdmin.warn("Export cannot find ArchiveExportOption:" + key);
                }
            }
        } catch (JSONException e) {
            throw new EngineException("Export unable to parse JSON exportOptions: " + exportOptionsTxt);
        }
    }
    // build a new CAR file from project directory
    f = CarUtils.makeArchive(projectName, exportOptions);
    // upload CAR file to admin
    HeaderName.ContentLength.setHeader(response, "" + f.length());
    if (f.exists()) {
        if (lastDate > 0) {
            f.setLastModified(lastDate);
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
        OutputStream outStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int nbReadBytes;
        while ((nbReadBytes = bis.read(buffer, 0, 1024)) > 0) {
            outStream.write(buffer, 0, nbReadBytes);
        }
        bis.close();
    }
    Engine.logAdmin.info("The project '" + projectName + "' has been exported");
}
Also used : OutputStream(java.io.OutputStream) EngineException(com.twinsoft.convertigo.engine.EngineException) ArchiveExportOption(com.twinsoft.convertigo.engine.enums.ArchiveExportOption) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) EngineException(com.twinsoft.convertigo.engine.EngineException) FileInputStream(java.io.FileInputStream) JSONObject(org.codehaus.jettison.json.JSONObject) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File)

Example 3 with ArchiveExportOption

use of com.twinsoft.convertigo.engine.enums.ArchiveExportOption in project convertigo by convertigo.

the class ArchiveExportOptionDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite composite = (Composite) super.createDialogArea(parent);
    GridLayout gl = new GridLayout(1, false);
    gl.verticalSpacing = 10;
    composite.setLayout(gl);
    Label label = new Label(composite, SWT.NONE);
    label.setText("You can update the version of your project before export or deployment.");
    Group group = new Group(composite, SWT.NONE);
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    group.setText("If you wish to, please change the value below: ");
    group.setLayout(new FillLayout(SWT.VERTICAL));
    versionSWT = new Text(group, SWT.NONE);
    versionSWT.setText(version);
    group = new Group(composite, SWT.NONE);
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    group.setText("Check to include: ");
    group.setLayout(new FillLayout(SWT.VERTICAL));
    archiveExportOptionsSWT = new Button[ArchiveExportOption.values().length];
    int i = 0;
    File projectDir = project.getDirFile();
    for (ArchiveExportOption option : ArchiveExportOption.values()) {
        long size = option.size(projectDir);
        if (size > 0) {
            Button check = new Button(group, SWT.CHECK);
            check.setData(option);
            if (option == ArchiveExportOption.includeTestCase) {
                check.setText(option.display());
            } else {
                check.setText(option.display() + " [" + FileUtils.byteCountToDisplaySize(size) + "]");
            }
            check.setSelection(archiveExportOptions.contains(option));
            archiveExportOptionsSWT[i++] = check;
        } else {
            archiveExportOptionsSWT[i++] = null;
        }
    }
    IApplicationComponent app = project.getMobileApplication() != null ? project.getMobileApplication().getApplicationComponent() : null;
    String msg = app != null ? app.getUnbuiltMessage() : null;
    if (msg != null) {
        label = new Label(composite, SWT.NONE);
        label.setText(msg + "\nOnce the build finished, you can " + (bDeploy ? "deploy" : "export") + " again.");
        SwtUtils.applyStyle(label, "{ color: red }");
    }
    composite.pack(true);
    return composite;
}
Also used : Group(org.eclipse.swt.widgets.Group) Composite(org.eclipse.swt.widgets.Composite) Label(org.eclipse.swt.widgets.Label) ArchiveExportOption(com.twinsoft.convertigo.engine.enums.ArchiveExportOption) Text(org.eclipse.swt.widgets.Text) FillLayout(org.eclipse.swt.layout.FillLayout) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) File(java.io.File) IApplicationComponent(com.twinsoft.convertigo.beans.core.IApplicationComponent)

Example 4 with ArchiveExportOption

use of com.twinsoft.convertigo.engine.enums.ArchiveExportOption in project convertigo by convertigo.

the class ExportOptions method getServiceResult.

protected void getServiceResult(HttpServletRequest request, Document document) throws Exception {
    String projectName = request.getParameter("projectName");
    Project project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName);
    File projectDir = project.getDirFile();
    Element root = document.getDocumentElement();
    Element options = document.createElement("options");
    for (ArchiveExportOption o : ArchiveExportOption.values()) {
        long size = o.size(projectDir);
        if (size > 0) {
            Element e = document.createElement("option");
            e.setAttribute("name", o.name());
            if (o == ArchiveExportOption.includeTestCase) {
                e.setAttribute("display", o.display());
            } else {
                e.setAttribute("display", o.display() + " [" + FileUtils.byteCountToDisplaySize(size) + "]");
            }
            options.appendChild(e);
        }
    }
    ;
    root.appendChild(options);
}
Also used : Project(com.twinsoft.convertigo.beans.core.Project) Element(org.w3c.dom.Element) ArchiveExportOption(com.twinsoft.convertigo.engine.enums.ArchiveExportOption) File(java.io.File)

Aggregations

ArchiveExportOption (com.twinsoft.convertigo.engine.enums.ArchiveExportOption)4 File (java.io.File)4 EngineException (com.twinsoft.convertigo.engine.EngineException)2 IOException (java.io.IOException)2 IApplicationComponent (com.twinsoft.convertigo.beans.core.IApplicationComponent)1 Project (com.twinsoft.convertigo.beans.core.Project)1 IntrospectionException (java.beans.IntrospectionException)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 FileSystem (java.nio.file.FileSystem)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 JSONException (org.codehaus.jettison.json.JSONException)1 JSONObject (org.codehaus.jettison.json.JSONObject)1 FillLayout (org.eclipse.swt.layout.FillLayout)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Button (org.eclipse.swt.widgets.Button)1