001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     tdelprat
018 *
019 */
020package org.nuxeo.wizard.helpers;
021
022import java.io.File;
023import java.io.FileWriter;
024import java.io.IOException;
025
026import org.nuxeo.launcher.config.ConfigurationGenerator;
027import org.nuxeo.wizard.context.Context;
028import org.nuxeo.wizard.context.ParamCollector;
029
030public class ConnectRegistrationHelper {
031
032    public static boolean isConnectRegistrationFileAlreadyPresent(Context ctx) {
033        String connectRegistrationFilePath = getConnectRegistrationFile(ctx);
034        return new File(connectRegistrationFilePath).exists();
035    }
036
037    public static String getConnectRegistrationFile(Context ctx) {
038        ParamCollector collector = ctx.getCollector();
039        ConfigurationGenerator cg = collector.getConfigurationGenerator();
040        String regTargetPath = cg.getDataDir().getAbsolutePath(); // cg.getRuntimeHome();
041
042        if (!regTargetPath.endsWith("/")) {
043            regTargetPath = regTargetPath + "/";
044        }
045
046        String connectRegistrationFilePath = regTargetPath + "instance.clid";
047
048        return connectRegistrationFilePath;
049    }
050
051    public static void saveConnectRegistrationFile(Context ctx) throws IOException {
052
053        String connectRegistrationFilePath = getConnectRegistrationFile(ctx);
054
055        String CLID1 = Context.getConnectMap().get("CLID").split("--")[0];
056        String CLID2 = Context.getConnectMap().get("CLID").split("--")[1];
057        String regFileContent = CLID1 + "\n" + CLID2 + "\nnew instance";
058
059        File regFile = new File(connectRegistrationFilePath);
060        try (FileWriter writer = new FileWriter(regFile)) {
061            writer.write(regFileContent);
062        }
063    }
064
065}