All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metaeffekt.mirror.contents.msrcdata.MsrcProduct Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.metaeffekt.mirror.contents.msrcdata;

import com.metaeffekt.artifact.analysis.utils.StringUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MsrcProduct {

    private final static Logger LOG = LoggerFactory.getLogger(MsrcProduct.class);

    private final String id, name, vendor, family;

    public MsrcProduct(String id, String name, String vendor, String family) {
        if (StringUtils.isEmpty(id)) {
            LOG.warn("Product ID is empty on MS product");
        }

        this.id = id;
        this.name = name;
        this.vendor = vendor;
        this.family = family;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getVendor() {
        return vendor;
    }

    public String getFamily() {
        return family;
    }

    public JSONObject toJson() {
        final JSONObject json = new JSONObject();

        json.put("id", id);
        json.put("name", name);
        json.put("vendor", vendor);
        json.put("family", family);

        return json;
    }

    @Override
    public String toString() {
        return toJson().toString();
    }

    public Document toDocument() {
        final Document doc = new Document();

        if (id != null) doc.add(new TextField("id", id, Store.YES));
        if (name != null) doc.add(new TextField("name", name, Store.YES));
        if (vendor != null) doc.add(new TextField("vendor", vendor, Store.YES));
        if (family != null) doc.add(new TextField("family", family, Store.YES));

        return doc;
    }

    public static MsrcProduct fromJson(JSONObject json) {
        return new MsrcProduct(
                json.optString("id", null),
                json.optString("name", null),
                json.optString("vendor", null),
                json.optString("family", null)
        );
    }

    public static MsrcProduct fromDocument(Document doc) {
        return new MsrcProduct(
                doc.get("id"),
                doc.get("name"),
                doc.get("vendor"),
                doc.get("family")
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy