org.jlot.client.executor.InitCommandExecutor Maven / Gradle / Ivy
package org.jlot.client.executor;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import javax.inject.Inject;
import org.jlot.client.configuration.Console;
import org.jlot.client.configuration.ProjectConfiguration;
import org.jlot.client.executor.spi.AbstractCommandExecutor;
import org.jlot.client.remote.ProjectAddRestCommand;
import org.jlot.client.remote.PushRestCommand;
import org.jlot.client.remote.rest.RestException;
import org.jlot.core.dto.PushResultDTO;
import org.jlot.core.form.ProjectForm;
import org.jlot.core.form.PushForm;
import org.jlot.core.utils.VersionResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class InitCommandExecutor extends AbstractCommandExecutor
{
protected final Logger log = LoggerFactory.getLogger(InitCommandExecutor.class);
@Inject
private ProjectAddRestCommand projectAddRestCommand;
@Inject
private ProjectConfiguration projectConfiguration;
@Inject
private Console console;
@Inject
private PushRestCommand pushRestCommand;
@Inject
private VersionResolver versionResolver;
@Override
public boolean executeInternal ( Properties properties ) throws RestException
{
initProject(properties);
push(properties);
return true;
}
private void initProject ( Properties properties ) throws RestException
{
Locale locale = getDefaultLocale(properties);
// setup projectForm
ProjectForm projectForm = new ProjectForm();
projectForm.setJlotClientVersion(versionResolver.getJlotVersionName());
projectForm.setLocale(locale);
projectForm.setName(projectConfiguration.getProjectName());
projectForm.setEncoding(projectConfiguration.getEncoding());
projectForm.setMessageFormat(projectConfiguration.isMessageFormat());
// remote: add Project
projectAddRestCommand.execute(projectForm);
log.info("Project successful created on {}", projectConfiguration.getServerUrl());
}
private void push ( Properties properties ) throws RestException
{
log.info("Pushing Sources and Translations");
PushForm pushForm = getPushCommandForm();
List resultList = pushRestCommand.execute(pushForm);
console.prompt("List of pushed files:");
for (PushResultDTO pushResultDTO : resultList)
{
console.prompt(pushResultDTO.getResourceName());
}
console.prompt("Pushing files done.");
}
protected PushForm getPushCommandForm ( )
{
PushForm pushForm = new PushForm();
pushForm.setJlotClientVersion(versionResolver.getJlotVersionName());
pushForm.setBaseDir(projectConfiguration.getBasedir());
pushForm.setProjectName(projectConfiguration.getProjectName());
pushForm.setVersionName(projectConfiguration.getVersionName());
pushForm.setResourceNameSet(projectConfiguration.getResources());
pushForm.setPushTranslations(true);
return pushForm;
}
private Locale getDefaultLocale ( Properties properties )
{
String localeString = (String) properties.get("defaultLocale");
if (localeString == null)
{
localeString = getDefaultLocaleFromConsole();
}
return StringUtils.parseLocaleString(localeString);
}
private String getDefaultLocaleFromConsole ( )
{
// get default locale
console.newline();
console.prompt("form.project.intro.api", projectConfiguration.getProjectName(), projectConfiguration.getServerUrl());
console.newline();
console.prompt("form.project.locale.help");
String localeString = console.readLine("form.project.locale.label", Locale.getDefault().toString());
if (!StringUtils.hasText(localeString))
{
localeString = Locale.getDefault().toString();
}
console.newline();
return localeString;
}
}