Exemplo n.º 1
0
// Add adds a raster to the database.
func (d *distros) add(ras *jdh.Raster) (string, error) {
	id := strconv.FormatInt(d.next, 10)
	ras.Id = id
	if err := d.validate(ras); err != nil {
		return "", err
	}
	d.addRaster(ras)
	d.next++
	d.changed = true
	return id, nil
}
Exemplo n.º 2
0
// Validate validates that a raster is valid in the database, and set some
// canonical values. It returns an error if the raster is not valid.
func (d *distros) validate(ras *jdh.Raster) error {
	ras.Id = strings.TrimSpace(ras.Id)
	ras.Taxon = strings.TrimSpace(ras.Taxon)
	if (len(ras.Id) == 0) || (len(ras.Taxon) == 0) {
		return errors.New("raster without identification")
	}
	if _, ok := d.ids[ras.Id]; ok {
		return fmt.Errorf("raster id %s already in use", ras.Id)
	}
	if !d.db.t.isInDB(ras.Taxon) {
		return fmt.Errorf("taxon %s [associated with raster %s] not in dabtase", ras.Taxon, ras.Id)
	}
	if ras.Cols == 0 {
		return fmt.Errorf("raster %s with an invalid number of cols: 0", ras.Id)
	}
	if ras.Raster == nil {
		return fmt.Errorf("raster %s without rasterized data", ras.Id)
	}
	if ras.Source > jdh.MachineModel {
		ras.Source = jdh.UnknownRaster
	}
	ras.Reference = strings.TrimSpace(ras.Reference)
	ext := ras.Extern
	ras.Extern = nil
	for _, e := range ext {
		serv, id, err := jdh.ParseExtern(e)
		if err != nil {
			continue
		}
		if len(id) == 0 {
			continue
		}
		add := true
		for _, ex := range ras.Extern {
			if strings.HasPrefix(ex, serv) {
				add = false
				break
			}
		}
		if !add {
			continue
		}
		if _, ok := d.ids[e]; !ok {
			ras.Extern = append(ras.Extern, e)
		}
	}
	return nil
}