Esempio n. 1
0
MojErr MojDbMultiExtractor::fromObject(const MojObject& obj, const MojChar* locale)
{
    LOG_TRACE("Entering function %s", __FUNCTION__);

	MojErr err = MojDbExtractor::fromObject(obj, locale);
	MojErrCheck(err);

	// load the default config
	MojDbPropExtractor defaultExtractor;
	err = defaultExtractor.fromObject(obj, locale);
	MojErrCheck(err);

	MojObject include;
	err = obj.getRequired(IncludeKey, include);
	MojErrCheck(err);

	for (MojObject::ConstArrayIterator i = include.arrayBegin();
		 i != include.arrayEnd(); ++i) {
		MojString includeName;
		err = i->stringValue(includeName);
		MojErrCheck(err);
		MojRefCountedPtr<MojDbPropExtractor> propExtractor(new MojDbPropExtractor);
		MojAllocCheck(propExtractor.get());
		err = propExtractor->fromObjectImpl(*i, defaultExtractor, locale);
		MojErrCheck(err);
		err = m_extractors.push(propExtractor);
		MojErrCheck(err);
	}
	return MojErrNone;
}
Esempio n. 2
0
MojErr MojDbDistinctTest::check(MojDb& db, const MojDbQuery& query, MojDbCursor& cursor, const MojChar* queryString, const MojChar* expectedIdsJson)
{
	MojErr err = db.find(query, cursor);
	MojTestErrCheck(err);

	MojObjectBuilder builder;
	err = builder.beginArray();
	MojTestErrCheck(err);
	err = cursor.visit(builder);
	MojTestErrCheck(err);
	err = cursor.close();
	MojTestErrCheck(err);
	err = builder.endArray();
	MojTestErrCheck(err);
	MojObject results = builder.object();

	MojString json;
	err = results.toJson(json);
	MojTestErrCheck(err);

	MojObject expected;
	err = expected.fromJson(expectedIdsJson);
	MojTestErrCheck(err);

	// size check
	MojTestAssert(expected.size() == results.size());

	// value check
	MojObject::ConstArrayIterator j = results.arrayBegin();
	for (MojObject::ConstArrayIterator i = expected.arrayBegin();
			i != expected.arrayEnd(); ++i, ++j) {
		MojObject value;
		err = j->getRequired(queryString, value);
		MojTestErrCheck(err);
		MojTestAssert(*i == value);
	}
	return MojErrNone;
}
Esempio n. 3
0
MojErr MojDbSearchTest::check(MojDb& db, const MojDbQuery& query, const MojChar* expectedIdsJson)
{
   MojString str;
   MojDbSearchCursor cursor(str);
	MojErr err = db.find(query, cursor);
	MojTestErrCheck(err);

	MojObjectBuilder builder;
	err = builder.beginArray();
	MojTestErrCheck(err);
	err = cursor.visit(builder);
	MojTestErrCheck(err);
	err = cursor.close();
	MojTestErrCheck(err);
	err = builder.endArray();
	MojTestErrCheck(err);
	MojObject results = builder.object();

	MojString json;
	err = results.toJson(json);
	MojTestErrCheck(err);

	MojObject expected;
	err = expected.fromJson(expectedIdsJson);
	MojTestErrCheck(err);

	MojTestAssert(expected.size() == results.size());
	MojObject::ConstArrayIterator j = results.arrayBegin();
	for (MojObject::ConstArrayIterator i = expected.arrayBegin();
			i != expected.arrayEnd(); ++i, ++j) {
		MojObject id;
		err = j->getRequired(MojDb::IdKey, id);
		MojTestErrCheck(err);
		MojTestAssert(*i == id);
	}
	return MojErrNone;
}
MojErr MovePopEmailsCommand::GetEmailsToMoveResponse(MojObject& response, MojErr err)
{
	try {
		ErrorToException(err);

		MojLogInfo(m_log, "response to get emails to move: %s", AsJsonString(response).c_str());

		MojObject results;
		err = response.getRequired(_T("results"), results);
		ErrorToException(err);

		MojObject::ObjectVec movedEmails;
		PopClient::AccountPtr account = m_client.GetAccount();

		for (MojObject::ConstArrayIterator it = results.arrayBegin(); it != results.arrayEnd(); it++) {
			MojObject singleMovedEmail;

			MojObject id;
			err = it->getRequired(PopEmailAdapter::ID, id);
			ErrorToException(err);
			MojObject srcFolderId;
			err = it->getRequired(EmailSchema::FOLDER_ID, srcFolderId);
			ErrorToException(err);
			MojObject destFolderId;
			err = it->getRequired("destFolderId", destFolderId);
			ErrorToException(err);
			MojObject flags;
			err = it->getRequired("flags", flags);
			ErrorToException(err);

			// set to be visible
			err = flags.put("visible", true);
			ErrorToException(err);

			// setup moved email, push onto moved emails vector
			err = singleMovedEmail.put(PopEmailAdapter::ID, id);
			ErrorToException(err);
			err = singleMovedEmail.put("folderId", destFolderId);
			ErrorToException(err);
			err = singleMovedEmail.put("destFolderId", MojObject());
			ErrorToException(err);
			err = singleMovedEmail.put("flags", flags);
			ErrorToException(err);
			// add emails to be moved into 'movedEmails' list
			movedEmails.push(singleMovedEmail);

			if (account->GetInboxFolderId() == srcFolderId) {
				// add moved inbox emails inot 'm_inboxEmailsMoved' set
				MojString uid;
				err = it->getRequired(PopEmailAdapter::SERVER_UID, uid);
				ErrorToException(err);

				m_inboxEmailsMoved.insert(std::string(uid));
			}
		}

		m_client.GetDatabaseInterface().UpdateItems(m_emailsMovedSlot, movedEmails);
	} catch (const std::exception& ex) {
		m_msg->replyError(MojErrInternal, ex.what());
		Failure(ex);
	} catch (...) {
		m_msg->replyError(MojErrInternal);
		Failure(MailException("unknown exception", __FILE__, __LINE__));
	}

	return MojErrNone;
}
Esempio n. 5
0
MojErr MojDbKind::configureIndexes(const MojObject& obj, const MojString& locale, MojDbReq& req)
{
	MojLogTrace(s_log);

	// make sure indexes changes count against our usage
	MojErr err = req.curKind(this);
	MojErrCheck(err);

	// add default id index to set
	MojObject idIndex;
	err = idIndex.fromJson(IdIndexJson);
	MojErrCheck(err);
	ObjectSet newIndexObjects;
	err = newIndexObjects.put(idIndex);
	MojErrCheck(err);
	// change back to a set and use contains
	MojSet<MojString> indexNames;
	MojString defaultIdxName;
	err = defaultIdxName.assign(IdIndexName);
	MojErrCheck(err);
	err = indexNames.put(defaultIdxName);
	MojErrCheck(err);
	// add indexes to set to uniquify and order them
	MojObject indexArray;
	if (obj.get(IndexesKey, indexArray)) {
		MojObject::ConstArrayIterator end = indexArray.arrayEnd();
		for (MojObject::ConstArrayIterator i = indexArray.arrayBegin(); i != end; ++i) {
			MojString indexName;
			err = i->getRequired(MojDbIndex::NameKey, indexName);
			MojErrCheck(err);
			err = indexName.toLower();
			MojErrCheck(err);
			if (!indexNames.contains(indexName)) {
				MojObject idx = *i;
				// make sure we keep the lower-cased index name
				err = idx.putString(MojDbIndex::NameKey, indexName);
				MojErrCheck(err);
				err = newIndexObjects.put(idx);
				MojErrCheck(err);
				err = indexNames.put(indexName);
				MojErrCheck(err);
			} else {
				MojErrThrowMsg(MojErrDbInvalidIndexName, _T("db: cannot repeat index name: '%s'"), indexName.data());
			}
		}
	}
	// figure out what to add and what to delete
	ObjectSet toDrop;
	err = m_indexObjects.diff(newIndexObjects, toDrop);
	MojErrCheck(err);
	ObjectSet toAdd;
	err = newIndexObjects.diff(m_indexObjects, toAdd);
	MojErrCheck(err);
	// drop deleted indexes
	IndexVec newIndexes;
	for (IndexVec::ConstIterator i = m_indexes.begin(); i != m_indexes.end(); ++i) {
		if (toDrop.contains((*i)->object())) {
			err = dropIndex(i->get(), req);
			MojErrCheck(err);
		} else {
			err = newIndexes.push(*i);
			MojErrCheck(err);
		}
	}
	// add new indexes
	for (ObjectSet::ConstIterator i = toAdd.begin(); i != toAdd.end(); ++i) {
		// create index
		MojRefCountedPtr<MojDbIndex> index(new MojDbIndex(this, m_kindEngine));
		MojAllocCheck(index.get());
		err = index->fromObject(*i, locale);
		MojErrCheck(err);
		// open index
		err = openIndex(index.get(), req);
		MojErrCheck(err);
		err = newIndexes.push(index);
		MojErrCheck(err);
	}
	// sort indexes by the prop vec so that for indexes that share prop prefixes, the shortest one comes first
	err = newIndexes.sort();
	MojErrCheck(err);
	// update members
	m_indexObjects = newIndexObjects;
	m_indexes = newIndexes;

	return MojErrNone;
}
Esempio n. 6
0
MojErr MojDbKind::configure(const MojObject& obj, const KindMap& map, const MojString& locale, MojDbReq& req)
{
	MojLogTrace(s_log);

	// get owner before checking permissions
	MojString owner;
	MojErr err = obj.getRequired(OwnerKey, owner);
	MojErrCheck(err);
	// only admin can change owner
	if (!m_owner.empty() && m_owner != owner && !req.admin()) {
		err = deny(req);
		MojErrCheck(err);
	}
	m_owner = owner;

	err = checkPermission(OpKindUpdate, req);
	MojErrCheck(err);

	// schema
	MojObject schema;
	if (obj.get(SchemaKey,schema)) {
		err = m_schema.fromObject(schema);
		MojErrCheck(err);
	}
	// supers
	StringVec superIds;
	MojObject supers;
	if (obj.get(ExtendsKey, supers)) {
		MojObject::ConstArrayIterator end = supers.arrayEnd();
		for (MojObject::ConstArrayIterator i = supers.arrayBegin(); i != end; ++i) {
			MojString str;
			err = i->stringValue(str);
			MojErrCheck(err);
			err = superIds.push(str);
			MojErrCheck(err);
		}
	}
	// backup
	bool backup = false;
	if (obj.get(SyncKey, backup))
		m_backup = backup;
	bool updating = !m_obj.undefined();

	// load state
	m_state.reset(new MojDbKindState(m_id, m_kindEngine));
	MojAllocCheck(m_state.get());
	err = m_state->init(m_schema.strings(), req);
	MojErrCheck(err);
	// indexes
	err = configureIndexes(obj, locale, req);
	MojErrCheck(err);
	// supers
	err = updateSupers(map, superIds, updating, req);
	MojErrCheck(err);
	// revision sets
	err = configureRevSets(obj);
	MojErrCheck(err);

	// keep a copy of obj
	m_obj = obj;

	return MojErrNone;
}