# Path: server/materialsdatabank/server/constants.py
# ELEMENT_SYMBOLS_LOWER = [x.lower() for x in ELEMENT_SYMBOLS]
# 
# ELEMENT_SYMBOLS = [
#   'Xx', 'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F',
#   'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K',
#   'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu',
#   'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y',
#   'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In',
#   'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr',
#   'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm',
#   'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au',
#   'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac',
#   'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es',
#   'Fm', 'Md', 'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt',
#   'Ds', 'Rg', 'Cn', 'Uut', 'Uuq', 'Uup', 'Uuh', 'Uus', 'Uuo'
# ]


import re
import datetime
from bson.objectid import ObjectId, InvalidId
from girder.models.model_base import AccessControlledModel
from girder.constants import AccessType
from girder.models.model_base import ValidationException
from girder.models.group import Group
from girder.models.item import Item
from girder.models.file import File
from girder import events
from girder.plugins.materialsdatabank.models.slug import Slug, SlugUpdateException
from girder.plugins.materialsdatabank.models.reconstruction import Reconstruction as ReconstructionModel
from girder.plugins.materialsdatabank.models.structure import Structure as StructureModel
from girder.plugins.materialsdatabank.models.projection import Projection as ProjectionModel
from ..constants import ELEMENT_SYMBOLS_LOWER, ELEMENT_SYMBOLS
        for s in species:
            if len(s) <= _chars_left():
                prefix.append(s)

        prefix += ['X'] * _chars_left()

        return ''.join(prefix)

    def _generate_mdb_id_postfix(self, prefix):
        # Search for existing datasets with this prefix
        regex = re.compile('^%s(\d{5})' % prefix)
        query = {
            'mdbId': {
                '$regex': regex
            }
        }

        cursor = super(Dataset, self).find(query, fields=['mdbId'])
        postfix = 0
        for d in cursor:
            match = regex.match(d['mdbId'])
            p = int(match.group(1))
            if  p > postfix:
                postfix = p

        postfix += 1

        return str(postfix).zfill(5)

    def ensure_mdb_id(self, dataset, species, updates):

# Path: server/materialsdatabank/server/constants.py
# ELEMENT_SYMBOLS_LOWER = [x.lower() for x in ELEMENT_SYMBOLS]
# 
# ELEMENT_SYMBOLS = [
#   'Xx', 'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F',
#   'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K',
#   'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu',
#   'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y',
#   'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In',
#   'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr',
#   'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm',
#   'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au',
#   'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac',
#   'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es',
#   'Fm', 'Md', 'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt',
#   'Ds', 'Rg', 'Cn', 'Uut', 'Uuq', 'Uup', 'Uuh', 'Uus', 'Uuo'
# ]


# Path: server/materialsdatabank/server/models/dataset.py
import re
import datetime
from bson.objectid import ObjectId, InvalidId
from girder.models.model_base import AccessControlledModel
from girder.constants import AccessType
from girder.models.model_base import ValidationException
from girder.models.group import Group
from girder.models.item import Item
from girder.models.file import File
from girder import events
from girder.plugins.materialsdatabank.models.slug import Slug, SlugUpdateException
from girder.plugins.materialsdatabank.models.reconstruction import Reconstruction as ReconstructionModel
from girder.plugins.materialsdatabank.models.structure import Structure as StructureModel
from girder.plugins.materialsdatabank.models.projection import Projection as ProjectionModel
from ..constants import ELEMENT_SYMBOLS_LOWER, ELEMENT_SYMBOLS

        for s in species:
            if len(s) <= _chars_left():
                prefix.append(s)

        prefix += ['X'] * _chars_left()

        return ''.join(prefix)

    def _generate_mdb_id_postfix(self, prefix):
        # Search for existing datasets with this prefix
        regex = re.compile('^%s(\d{5})' % prefix)
        query = {
            'mdbId': {
                '$regex': regex
            }
        }

        cursor = super(Dataset, self).find(query, fields=['mdbId'])
        postfix = 0
        for d in cursor:
            match = regex.match(d['mdbId'])
            p = int(match.group(1))
            if  p > postfix:
                postfix = p

        postfix += 1

        return str(postfix).zfill(5)

    def ensure_mdb_id(self, dataset, species, updates):

        species = [ELEMENT_SYMBOLS[n] for n in species]