func (r *Record) GetDependantReferenceMaps(connection *database.Connection) []ReferenceMap {

	endpoint_id := r.EndpointId
	application_id := r.ApplicationId

	endpoint := r.GetEndpointFromCache()

	var results []ReferenceMap

	if endpoint.CachedSourceReferenceMaps {
		logger.Debug("[Cache][Reference Maps][Source] Hit: ", r.EndpointId.Hex())
		results = endpoint.SourceReferencedMaps
	} else {
		logger.Debug("[Cache][Reference Maps][Source] Miss: ", r.EndpointId.Hex())
		query := map[string]bson.ObjectId{"source_id": endpoint_id}

		// Get the collection name
		collection := application_id.Hex() + "_maps"

		// Query the database
		database.Query(connection, collection, query).All(&results)

		if endpoint.Id != bson.ObjectId("") {
			key := r.ApplicationId.Hex() + ".endpoints." + endpoint_id.Hex()
			endpoint.SourceReferencedMaps = results
			endpoint.CachedSourceReferenceMaps = true
			b, _ := json.Marshal(endpoint)
			cache.Set(key, string(b), 5*time.Minute)

			logger.Debug("[Cache][Reference Maps][Source] Set: ", r.EndpointId.Hex())
		}
	}

	return results
}
Exemple #2
0
func (endpoint Endpoint) FilesForRecords(records []Record) []File {
	result := []File{}

	ids := []bson.ObjectId{}

	for _, record := range records {
		if record.FileIds != nil {
			ids = append(ids, record.FileIds...)
		}
	}

	if len(ids) != 0 {
		query := bson.M{"_id": map[string]interface{}{"$in": ids}}

		collection := endpoint.ApplicationId.Hex()
		collection += "_" + endpoint.Id.Hex()
		collection += "_files"

		connection := database.GetConnection()
		database.Query(connection, collection, query).All(&result)
		connection.Release()
	}

	return result
}
func (destination *Record) UpdateReferenceMap(connection *database.Connection, r ReferenceMap) {
	query := r.GenerateQueryForRecord(destination)
	var source Record
	collection := destination.ApplicationId.Hex() + "_" + r.SourceId.Hex()

	database.Query(connection, collection, query).One(&source)
	if source.Id != bson.ObjectId("") {
		r.UpdateDestionationWithSource(destination, &source)
	}
}
Exemple #4
0
func (application Application) FindUsersByIds(ids []bson.ObjectId) []User {
	results := []User{}

	in := map[string]interface{}{"$in": ids}

	query := map[string]interface{}{"_id": in}

	connection := database.GetConnection()
	defer connection.Release()

	database.Query(connection, application.Id.Hex()+"_users", query).All(&results)

	return results
}
func (r *Record) UpdateUnfulfilledObjectsForReference(connection *database.Connection, reference_map ReferenceMap) {
	reference_map_id := []bson.ObjectId{reference_map.Id}
	reference_id_query := map[string]interface{}{"$in": reference_map_id}
	not_query := map[string]interface{}{"$not": reference_id_query}

	query := map[string]interface{}{"references.fulfilled_reference_ids": not_query}

	var results []Record
	collection := r.ApplicationId.Hex() + "_" + reference_map.DestinationId.Hex()

	database.Query(connection, collection, query).All(&results)

	for _, destination := range results {
		if sourceAndReferenceMapApplicableForDestination(connection, r, reference_map, destination) {
			reference_map.UpdateDestionationWithSource(&destination, r)
		}
	}
}
func (application Application) AllEndpoints() []Endpoint {
	var results []Endpoint

	query_map := map[string]interface{}{}

	collection := application.Id.Hex() + "_endpoints"

	// Reserve a connection (And ensure we release it)
	connection := database.GetConnection()

	// Query the database
	database.Query(connection, collection, query_map).All(&results)

	// Release the database connection
	connection.Release()

	return results
}
// [Todo] Find a better way that does this that is able to avoid a call 100% of the time
func sourceAndReferenceMapApplicableForDestination(connection *database.Connection, source *Record, reference_map ReferenceMap, destination Record) bool {
	query := reference_map.GenerateQueryForRecord(&destination)

	ok := true
	check_db := false

	for key, value := range query {
		contains_dollar_sign := strings.Contains(key, "$")
		contains_dot := strings.Contains(key, ".")
		value_type := reflect.TypeOf(value).String()

		is_valid_type := value_type == "string" || value_type == "bson.ObjectId"

		if !contains_dollar_sign && !contains_dot && is_valid_type {
			var match interface{}

			if key == "created_by" {
				match = source.CreatedBy
			} else if key == "id" {
				match = source.Id
			} else {
				match = source.Content[key]
			}

			if match != value {
				ok = false
				break
			}

		} else {
			check_db = true
		}
	}

	if check_db && ok {
		var result Record
		collection := source.ApplicationId.Hex() + "_" + reference_map.SourceId.Hex()
		database.Query(connection, collection, query).One(&result)

		ok = result.Id == source.Id
	}

	return ok
}
func (source *Record) UpdateSourcedObjectsForReference(connection *database.Connection, reference_map ReferenceMap) {
	source_ids := []bson.ObjectId{source.Id}
	source_id_query := map[string]interface{}{"$in": source_ids}
	query := map[string]interface{}{"references.source_ids": source_id_query}

	var results []Record

	collection := source.ApplicationId.Hex() + "_" + reference_map.DestinationId.Hex()

	database.Query(connection, collection, query).All(&results)

	for _, destination := range results {
		if sourceAndReferenceMapApplicableForDestination(connection, source, reference_map, destination) {
			reference_map.UpdateDestionationWithSource(&destination, source)
		} else {
			reference_map.RemoveReferenceDataFromDestination(&destination, source)
		}
	}
}
Exemple #9
0
func runCron() {
	results := []model.Application{}
	query_map := map[string]interface{}{}

	collection := "applications"

	// Reserve a connection (And ensure we release it)
	connection := database.GetConnection()

	// Query the database
	database.Query(connection, collection, query_map).All(&results)

	// Release the database connection
	connection.Release()

	for _, application := range results {
		application.ExecuteCronTrigger()
	}
}
Exemple #10
0
func (e Endpoint) findRecordsWhereWithCount(query map[string]interface{}, groups []bson.ObjectId, super_user bool, action string, should_count bool) ([]Record, int) {
	var results []Record

	// Handle the $show_count param
	if !should_count && query["$show_count"] != nil {
		if query["$show_count"].(bool) == true {
			should_count = true
		}

		delete(query, "$show_count")
	}

	// Get the collection name
	collection := e.ApplicationId.Hex() + "_" + e.Id.Hex()

	// Sanitize and Nest the Query
	query_map := sanitizeQueryAsNested(query, false)

	// Check if the user is covered by default groups
	if !super_user && !e.CanGroupsPerform(groups, action) {
		// If not add a restriction on the query
		find := query_map["$find"].(map[string]interface{})
		find["permissions."+action+"_group_ids"] = map[string]interface{}{"$in": groups}
		query_map["$find"] = find
	}

	// Reserve a connection (And ensure we release it)
	connection := database.GetConnection()

	// Query the database
	query_cursor := database.Query(connection, collection, query_map["$find"].(map[string]interface{}))

	count := 0

	if should_count {
		count, _ = query_cursor.Count()
	} else {
		count = -1
	}

	for key, value := range query_map {
		if key == "$find" {

		} else if key == "$sort" {
			keys := []string{}

			for key, v := range value.(map[string]interface{}) {

				object_type := reflect.TypeOf(v).String()

				n := int64(1)

				if object_type == "json.Number" {
					n, _ = (v.(json.Number)).Int64()
				} else if object_type == "int" {
					n = int64(v.(int))
				}

				if n == -1 {
					keys = append(keys, "-"+key)
				} else {
					keys = append(keys, key)
				}
			}

			query_cursor = query_cursor.Sort(keys...)
		} else if key == "$limit" {
			is := (value.(json.Number)).String()
			i, _ := strconv.Atoi(is)
			query_cursor = query_cursor.Limit(i)
		} else if key == "$skip" {
			is := (value.(json.Number)).String()
			i, _ := strconv.Atoi(is)
			query_cursor = query_cursor.Skip(i)
		} else if key == "$only" {
			//fields, _ := bson.Marshal(value.(map[string]interface{}))
			query_cursor = query_cursor.Select(value)
		}
	}

	query_cursor.All(&results)

	connection.Release()

	// Return our results
	return results, count
}