Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright 2012 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.usergrid.tools;
import static org.usergrid.persistence.Schema.PROPERTY_TYPE;
import static org.usergrid.persistence.Schema.PROPERTY_UUID;
import static org.usergrid.persistence.cassandra.CassandraService.MANAGEMENT_APPLICATION_ID;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.usergrid.management.ApplicationInfo;
import org.usergrid.management.OrganizationInfo;
import org.usergrid.management.UserInfo;
import org.usergrid.persistence.Entity;
import org.usergrid.persistence.EntityManager;
import org.usergrid.persistence.EntityRef;
import org.usergrid.persistence.Schema;
import org.usergrid.persistence.entities.Application;
import org.usergrid.persistence.exceptions.ApplicationAlreadyExistsException;
import org.usergrid.persistence.exceptions.DuplicateUniquePropertyExistsException;
import org.usergrid.tools.bean.ExportOrg;
import org.usergrid.utils.JsonUtils;
public class Import extends ToolBase {
private static final Logger logger = LoggerFactory.getLogger(Import.class);
/** Input directory where the .json export files are */
static final String INPUT_DIR = "inputDir";
static File importDir;
static final String DEFAULT_INPUT_DIR = "export";
JsonFactory jsonFactory = new JsonFactory();
@Override
@SuppressWarnings("static-access")
public Options createOptions() {
Option hostOption = OptionBuilder.withArgName("host").hasArg()
.withDescription("Cassandra host").create("host");
Option inputDir = OptionBuilder.hasArg()
.withDescription("input directory -inputDir").create(INPUT_DIR);
Option verbose = OptionBuilder
.withDescription(
"Print on the console an echo of the content written to the file")
.create(VERBOSE);
Options options = new Options();
options.addOption(hostOption);
options.addOption(inputDir);
options.addOption(verbose);
return options;
}
@Override
public void runTool(CommandLine line) throws Exception {
startSpring();
setVerbose(line);
openImportDirectory(line);
importOrganizations();
importApplications();
importCollections();
//forces the counters to flush
logger.info("Sleeping 30 seconds for batcher");
Thread.sleep(35000);
}
/**
* Import applications
*/
private void importApplications() throws Exception {
String[] nanemspaceFileNames = importDir.list(new PrefixFileFilter(
"application."));
logger.info("Applications to read: " + nanemspaceFileNames.length);
for (String applicationName : nanemspaceFileNames) {
try {
importApplication(applicationName);
} catch (Exception e) {
logger.warn("Unable to import application: " + applicationName,
e);
}
}
}
/**
* Imports a application
*
* @param applicationName
* file name where the application was exported.
*/
private void importApplication(String applicationName) throws Exception {
// Open up application file.
File applicationFile = new File(importDir, applicationName);
logger.info("Loading application file: "
+ applicationFile.getAbsolutePath());
JsonParser jp = getJsonParserForFile(applicationFile);
JsonToken token = jp.nextToken();
validateStartArray(token);
// Move to next object (the application).
// The application object is the first object followed by all the
// objects in this application.
token = jp.nextValue();
Application application = jp.readValueAs(Application.class);
@SuppressWarnings("unchecked")
String orgName = ((Map) application
.getMetadata("organization")).get("value");
OrganizationInfo info = managementService.getOrganizationByName(orgName);
if(info == null){
logger.error("Unable to import application '{}' for organisation with name '{}'", application.getName(), orgName);
return;
}
UUID appId = null;
try{
appId = managementService.importApplication(info.getUuid(), application);
}catch(ApplicationAlreadyExistsException aaee){
ApplicationInfo appInfo = managementService.getApplicationInfo(orgName+"/"+application.getName());
if(appInfo != null){
appId = appInfo.getId();
}
}
echo(application);
EntityManager em = emf.getEntityManager(appId);
// we now need to remove all roles, they'll be imported again below
for (Entry entry : em.getRoles().entrySet()) {
em.deleteRole(entry.getKey());
}
//load all the dictionaries
@SuppressWarnings("unchecked")
Map dictionaries = (Map) application.getMetadata("dictionaries");
if(dictionaries != null){
EntityManager rootEm = emf.getEntityManager(MANAGEMENT_APPLICATION_ID);
Entity appEntity = rootEm.get(appId);
for(Entry dictionary: dictionaries.entrySet()){
@SuppressWarnings("unchecked")
Map