Merge pull request #60 from GERey/Export_Persistance_Integration

Export persistance integration
diff --git a/.gitignore b/.gitignore
index eacfd46..2e2bcbe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,4 +20,11 @@
 /stack/corepersistence/priamcluster/aws.properties
 #Webstorm artifacts
 .idea/
-portal/2.0.**
\ No newline at end of file
+portal/2.0.**
+stack/corepersistence/collection/nbactions.xml
+stack/corepersistence/graph/nbactions.xml
+stack/corepersistence/model/nbactions.xml
+stack/corepersistence/nbactions.xml
+stack/corepersistence/queryindex/nbactions.xml
+stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/QueryFilterLexer.java
+stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/query/tree/QueryFilterParser.java
diff --git a/stack/pom.xml b/stack/pom.xml
index 1e4214c..93d57a0 100644
--- a/stack/pom.xml
+++ b/stack/pom.xml
@@ -1789,12 +1789,15 @@
                     <exclude>**/META-INF/**</exclude>
                     <exclude>**/dependency-reduced-pom.xml</exclude>
                     <exclude>**/QueryFilter.tokens</exclude>
+                    <exclude>**/QueryFilterLexer.java</exclude>
+                    <exclude>**/QueryFilterParser.java</exclude>
 
                     <!-- other -->
                     <exclude>**/m2/**</exclude>
                     <exclude>**/*.asc</exclude>
                     <exclude>**/src/test/resources/**</exclude>
                     <exclude>**/cloudbees.xml</exclude>
+                    <exclude>**/aws.properties</exclude>
 
                 </excludes>
             </configuration>
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java b/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java
index d7c7f9a..10c208f 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/AbstractCollectionService.java
@@ -295,6 +295,9 @@
         EntityRef ref = em.getAlias( getEntityType(), name );
         Entity entity;
         if ( ref == null ) {
+            // null entity ref means we tried to put a non-existing entity
+            // before we create a new entity for it, we should check for permission
+            checkPermissionsForCollection(context);
             Map<String, Object> properties = context.getPayload().getProperties();
             if ( !properties.containsKey( "name" ) || !( ( String ) properties.get( "name" ) ).trim().equalsIgnoreCase(
                     name ) ) {
diff --git a/stack/tools/src/main/java/org/apache/usergrid/tools/RepairingMismatchedApplicationMetadata.java b/stack/tools/src/main/java/org/apache/usergrid/tools/RepairingMismatchedApplicationMetadata.java
new file mode 100644
index 0000000..c0bd320
--- /dev/null
+++ b/stack/tools/src/main/java/org/apache/usergrid/tools/RepairingMismatchedApplicationMetadata.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.usergrid.tools;
+
+import com.google.common.collect.BiMap;
+import me.prettyprint.cassandra.serializers.ByteBufferSerializer;
+import me.prettyprint.hector.api.Keyspace;
+import me.prettyprint.hector.api.mutation.Mutator;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+
+import java.nio.ByteBuffer;
+import java.util.Map;
+import java.util.UUID;
+
+import static me.prettyprint.hector.api.factory.HFactory.createMutator;
+import static org.apache.usergrid.persistence.Schema.PROPERTY_NAME;
+import static org.apache.usergrid.persistence.Schema.PROPERTY_UUID;
+import static org.apache.usergrid.persistence.cassandra.CassandraPersistenceUtils.addInsertToMutator;
+import static org.apache.usergrid.persistence.cassandra.CassandraPersistenceUtils.batchExecute;
+import static org.apache.usergrid.persistence.cassandra.CassandraService.APPLICATIONS_CF;
+import static org.apache.usergrid.persistence.cassandra.CassandraService.RETRY_COUNT;
+
+public class RepairingMismatchedApplicationMetadata extends ToolBase {
+
+    public static final ByteBufferSerializer be = new ByteBufferSerializer();
+
+    @Override
+    public Options createOptions() {
+        Options options = super.createOptions();
+        return options;
+    }
+
+    @Override
+    public void runTool(CommandLine line) throws Exception {
+        startSpring();
+
+        BiMap<UUID, String> orgs = managementService.getOrganizations();
+        for(Map.Entry org : orgs.entrySet()) {
+            BiMap<UUID, String> apps = managementService.getApplicationsForOrganization((UUID)org.getKey());
+            for(Map.Entry app : apps.entrySet()) {
+                UUID applicationId = emf.lookupApplication((String)app.getValue());
+                if( applicationId == null ) {
+                    String appName = (String)app.getValue();
+                    Keyspace ko = cass.getSystemKeyspace();
+                    Mutator<ByteBuffer> m = createMutator(ko, be);
+                    long timestamp = cass.createTimestamp();
+                    addInsertToMutator(m, APPLICATIONS_CF, appName, PROPERTY_UUID, (UUID)app.getKey(), timestamp);
+                    addInsertToMutator(m, APPLICATIONS_CF, appName, PROPERTY_NAME, appName, timestamp);
+                    batchExecute(m, RETRY_COUNT);
+                    logger.info("UUID {}, NAME {}", app.getKey(), app.getValue());
+                }
+            }
+        }
+
+        logger.info("Waiting 60 sec...");
+        Thread.sleep(1000 * 60);
+    }
+}