Exemple #1
0
func UpsertOrder(o *Order) error {
	defer event_log.SaveShopEvent(event_log.ActionTest, &event_log.Info{OrderId: o.GetID()}, nil, "")
	// order is unlinked or not yet inserted in db

	if o.unlinkDB || o.BsonId == "" {
		return nil
	}
	p := GetOrderPersistor()

	// Get current version from db and check against verssion of c
	// If they are not identical, there must have been another upsert which would be overwritten by this one.
	// In this case upsert is skipped and an error is returned,
	orderLatestFromDb := &Order{}
	err := p.GetCollection().Find(&bson.M{"id": o.GetID()}).Select(&bson.M{"version": 1}).One(orderLatestFromDb)

	if err != nil {
		log.Println("Upsert failed: Could not find order with id", o.GetID(), "Error:", err)
		return err
	}

	latestVersionInDb := orderLatestFromDb.Version.GetVersion()
	if latestVersionInDb != o.Version.GetVersion() && !o.Flags.forceUpsert {
		errMsg := fmt.Sprintln("WARNING: Cannot upsert latest version ", strconv.Itoa(latestVersionInDb), "in db with version", strconv.Itoa(o.Version.GetVersion()), "!")
		log.Println(errMsg)
		return errors.New(errMsg)
	}

	if o.Flags.forceUpsert {
		// Remember this number, so that we later know from which version we came from
		v := o.Version.Current
		// Set the current version number to keep history consistent
		o.Version.Current = latestVersionInDb
		o.Version.Increment()
		o.Flags.forceUpsert = false
		// Overwrite NumberPrevious, to remember where we came from
		o.Version.Previous = v
	} else {
		o.Version.Increment()
	}

	o.State.SetModified()
	_, err = p.GetCollection().UpsertId(o.BsonId, o)
	if err != nil {
		return err
	}

	// Store version in history
	bsonId := o.BsonId
	o.BsonId = "" // Temporarily reset Mongo ObjectId, so that we can perfrom an Insert.
	pHistory := GetOrderVersionsPersistor()
	pHistory.GetCollection().Insert(o)
	o.BsonId = bsonId // restore bsonId
	//event_log.SaveShopEvent(event_log.ActionUpsertingOrder, o.GetID(), err, "")
	return err
}
Exemple #2
0
func foo() {
	info := &event_log.Info{}
	event_log.SaveShopEvent(event_log.ActionTest, info, nil, "bla bla bla")
	event_log.ReportShopEvents()
}