def create_style(request, json_data, layer_id, is_preview=False):

    layer = Layer.objects.get(id=int(layer_id))
    datastore = layer.datastore
    workspace = datastore.workspace
    gs = geographic_servers.get_instance().get_server_by_id(
        workspace.server.id)

    layer_styles = StyleLayer.objects.filter(layer=layer)
    is_default = False
    if not is_preview:
        is_default = json_data.get('is_default')
    if is_default:
        for ls in layer_styles:
            s = Style.objects.get(id=ls.style.id)
            s.is_default = False
            s.save()
    else:
        has_default_style = False
        for ls in layer_styles:
            s = Style.objects.get(id=ls.style.id)
            if s.is_default:
                has_default_style = True
        if not has_default_style:
            is_default = True

    name = json_data.get('name')
    if is_preview:
        name = name + '__tmp'
        is_default = False

    style = Style(name=name,
                  title=json_data.get('title'),
                  is_default=is_default,
                  type='IN')
    if json_data.get('minscale') != '':
        style.minscale = json_data.get('minscale')
    else:
        style.minscale = -1
    if json_data.get('maxscale') != '':
        style.maxscale = json_data.get('maxscale')
    else:
        style.maxscale = -1

    style.save()
    style_layer = StyleLayer(style=style, layer=layer)
    style_layer.save()

    for r in json_data.get('rules'):
        json_rule = r.get('rule')

        filter_text = ""
        if json_rule.get('filter').__len__() != 0:
            filter_text = str(json.dumps(json_rule.get('filter')))

        rule = Rule(style=style,
                    name=json_rule.get('name'),
                    title=json_rule.get('title'),
                    abstract='',
                    filter=filter_text,
                    minscale=json_rule.get('minscale'),
                    maxscale=json_rule.get('maxscale'),
                    order=json_rule.get('order'))
        rule.save()

        for sym in r.get('symbolizers'):
            if sym.get('type') == 'PolygonSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = PolygonSymbolizer(
                    rule=rule,
                    order=int(sym.get('order')),
                    fill=json_sym.get('fill'),
                    fill_opacity=json_sym.get('fill_opacity'),
                    stroke=json_sym.get('stroke'),
                    stroke_width=json_sym.get('stroke_width'),
                    stroke_opacity=json_sym.get('stroke_opacity'),
                    stroke_dash_array=json_sym.get('stroke_dash_array'))
                symbolizer.save()

            elif sym.get('type') == 'LineSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = LineSymbolizer(
                    rule=rule,
                    order=int(sym.get('order')),
                    stroke=json_sym.get('stroke'),
                    stroke_width=json_sym.get('stroke_width'),
                    stroke_opacity=json_sym.get('stroke_opacity'),
                    stroke_dash_array=json_sym.get('stroke_dash_array'))
                symbolizer.save()

            elif sym.get('type') == 'MarkSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = MarkSymbolizer(
                    rule=rule,
                    order=int(sym.get('order')),
                    opacity=json_sym.get('opacity'),
                    size=json_sym.get('size'),
                    rotation=json_sym.get('rotation'),
                    well_known_name=json_sym.get('well_known_name'),
                    fill=json_sym.get('fill'),
                    fill_opacity=json_sym.get('fill_opacity'),
                    stroke=json_sym.get('stroke'),
                    stroke_width=json_sym.get('stroke_width'),
                    stroke_opacity=json_sym.get('stroke_opacity'),
                    stroke_dash_array=json_sym.get('stroke_dash_array'))
                symbolizer.save()

            elif sym.get('type') == 'ExternalGraphicSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = ExternalGraphicSymbolizer(
                    rule=rule,
                    order=int(sym.get('order')),
                    opacity=json_sym.get('opacity'),
                    size=json_sym.get('size'),
                    rotation=json_sym.get('rotation'),
                    online_resource=json_sym.get('online_resource'),
                    format=json_sym.get('format'))
                symbolizer.save()

            elif sym.get('type') == 'TextSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = TextSymbolizer(
                    rule=rule,
                    order=int(sym.get('order')),
                    label=json_sym.get('label'),
                    font_family=json_sym.get('font_family'),
                    font_size=json_sym.get('font_size'),
                    font_weight=json_sym.get('font_weight'),
                    font_style=json_sym.get('font_style'),
                    halo_fill=json_sym.get('halo_fill'),
                    halo_fill_opacity=json_sym.get('halo_fill_opacity'),
                    halo_radius=json_sym.get('halo_radius'),
                    fill=json_sym.get('fill'),
                    fill_opacity=json_sym.get('fill_opacity'),
                )
                symbolizer.save()

    sld_body = sld_builder.build_sld(layer, style)

    if is_preview:
        if gs.createOverwrittenStyle(style.name, sld_body, True):
            return True
        else:
            return False
    else:
        if gs.createStyle(style.name, sld_body):
            if not is_preview:
                gs.setLayerStyle(layer, style.name, style.is_default)
            return True

        else:
            return False
def create_style(request, json_data, layer, gs, is_preview=False):
    name = json_data.get('name')
    if is_preview:
        name = name + '__tmp'
    is_default = json_data.get('is_default', False)
    is_default = utils.set_default_style(layer, gs,is_preview=is_preview, is_default=is_default)

    style = Style(
        name = name,
        title = json_data.get('title'),
        is_default = is_default,
        type = 'EX'
    )
    if json_data.get('minscale') != '':
        style.minscale = json_data.get('minscale')
    else:
        style.minscale = -1
    if json_data.get('maxscale') != '':
        style.maxscale = json_data.get('maxscale')
    else:
        style.maxscale = -1
    style.save()
    style_layer = StyleLayer(
        style = style,
        layer = layer
    )
    style_layer.save()

    for r in json_data.get('rules'):
        json_rule = r.get('rule')

        filter_text = ""
        if json_rule.get('filter').__len__() != 0:
            filter_text = str(json.dumps(json_rule.get('filter')))

        rule = Rule(
            style = style,
            name = json_rule.get('name'),
            title = json_rule.get('title'),
            abstract = '',
            filter = filter_text,
            minscale = json_rule.get('minscale'),
            maxscale = json_rule.get('maxscale'),
            order = json_rule.get('order')
        )
        rule.save()

        for sym in r.get('symbolizers'):
            if sym.get('type') == 'PolygonSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = PolygonSymbolizer(
                    rule = rule,
                    order = int(sym.get('order')),
                    fill = json_sym.get('fill'),
                    fill_opacity = json_sym.get('fill_opacity'),
                    stroke = json_sym.get('stroke'),
                    stroke_width = json_sym.get('stroke_width'),
                    stroke_opacity = json_sym.get('stroke_opacity'),
                    stroke_dash_array = json_sym.get('stroke_dash_array')
                )
                symbolizer.save()

            elif sym.get('type') == 'LineSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = LineSymbolizer(
                    rule = rule,
                    order = int(sym.get('order')),
                    stroke = json_sym.get('stroke'),
                    stroke_width = json_sym.get('stroke_width'),
                    stroke_opacity = json_sym.get('stroke_opacity'),
                    stroke_dash_array = json_sym.get('stroke_dash_array')
                )
                symbolizer.save()

            elif sym.get('type') == 'MarkSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = MarkSymbolizer(
                    rule = rule,
                    order = int(sym.get('order')),
                    opacity = json_sym.get('opacity'),
                    size = json_sym.get('size'),
                    rotation = json_sym.get('rotation'),
                    well_known_name = json_sym.get('well_known_name'),
                    fill = json_sym.get('fill'),
                    fill_opacity = json_sym.get('fill_opacity'),
                    stroke = json_sym.get('stroke'),
                    stroke_width = json_sym.get('stroke_width'),
                    stroke_opacity = json_sym.get('stroke_opacity'),
                    stroke_dash_array = json_sym.get('stroke_dash_array')
                )
                symbolizer.save()

            elif sym.get('type') == 'ExternalGraphicSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = ExternalGraphicSymbolizer(
                    rule = rule,
                    order = int(sym.get('order')),
                    opacity = json_sym.get('opacity'),
                    size = json_sym.get('size'),
                    rotation = json_sym.get('rotation'),
                    online_resource = json_sym.get('online_resource'),
                    format = json_sym.get('format')
                )
                symbolizer.save()

            elif sym.get('type') == 'TextSymbolizer':
                json_sym = json.loads(sym.get('json'))
                symbolizer = TextSymbolizer(
                    rule = rule,
                    order = int(sym.get('order')),
                    label = json_sym.get('label'),
                    font_family = json_sym.get('font_family'),
                    font_size = json_sym.get('font_size'),
                    font_weight = json_sym.get('font_weight'),
                    font_style = json_sym.get('font_style'),
                    halo_fill = json_sym.get('halo_fill'),
                    halo_fill_opacity = json_sym.get('halo_fill_opacity'),
                    halo_radius = json_sym.get('halo_radius'),
                    fill = json_sym.get('fill'),
                    fill_opacity = json_sym.get('fill_opacity'),

                )
                symbolizer.save()

    sld_body = sld_builder.build_sld(layer, style)
    if is_preview:
        if gs.createOverwrittenStyle(style.name, sld_body, True): 
            return style
    else:
        if gs.createStyle(style.name, sld_body): 
            if not is_preview:
                gs.setLayerStyle(layer, style.name, style.is_default)
            return style