<commit_before>/*
 * Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * 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 org.hawkular.inventory.base;

import static org.hawkular.inventory.api.Action.created;
import static org.hawkular.inventory.api.Relationships.Direction.outgoing;
import static org.hawkular.inventory.api.Relationships.WellKnown.contains;
import static org.hawkular.inventory.api.Relationships.WellKnown.defines;
import static org.hawkular.inventory.api.Relationships.WellKnown.incorporates;
import static org.hawkular.inventory.api.Relationships.WellKnown.isParentOf;
import static org.hawkular.inventory.api.filters.With.id;

import java.util.ArrayList;
import java.util.List;

import org.hawkular.inventory.api.Data;
import org.hawkular.inventory.api.EntityAlreadyExistsException;
import org.hawkular.inventory.api.EntityNotFoundException;
import org.hawkular.inventory.api.Metrics;
import org.hawkular.inventory.api.RelationAlreadyExistsException;
import org.hawkular.inventory.api.RelationNotFoundException;
import org.hawkular.inventory.api.Resources;
import org.hawkular.inventory.api.filters.Filter;
import org.hawkular.inventory.api.filters.RecurseFilter;
import org.hawkular.inventory.api.filters.Related;
import org.hawkular.inventory.api.filters.With;
import org.hawkular.inventory.api.model.DataEntity;
import org.hawkular.inventory.api.model.Metric;
import org.hawkular.inventory.api.model.Relationship;
import org.hawkular.inventory.api.model.Resource;
import org.hawkular.inventory.api.model.ResourceType;
import org.hawkular.inventory.base.spi.ElementNotFoundException;
import org.hawkular.inventory.paths.CanonicalPath;
import org.hawkular.inventory.paths.DataRole;
import org.hawkular.inventory.paths.Path;
import org.hawkular.inventory.paths.SegmentType;

/**
 * @author <NAME>
 * @since 0.1.0
 */
public final class BaseResources {

    private BaseResources() {

    }

    public static class ReadWrite<BE> extends Mutator<BE, Resource, Resource.Blueprint, Resource.Update, String>
            implements Resources.ReadWrite {

        public ReadWrite(TraversalContext<BE, Resource> context) {
            super(context);
        }

        @Override
        protected String getProposedId(Transaction<BE> tx, Resource.Blueprint blueprint) {
            return blueprint.getId();
        }

        @Override
        protected EntityAndPendingNotifications<BE, Resource> wireUpNewEntity(BE entity,
                                                                              Resource.Blueprint blueprint,
                                                                              CanonicalPath parentPath, BE parent,
                                                                              Transaction<BE> tx) {

            BE resourceTypeObject;
            CanonicalPath resourceTypePath = null;
            try {
                CanonicalPath tenant = CanonicalPath.of().tenant(parentPath.ids().getTenantId()).get();
                resourceTypePath = Util.canonicalize(blueprint.getResourceTypePath(), tenant,
                        parentPath, ResourceType.SEGMENT_TYPE);
                resourceTypeObject = tx.find(resourceTypePath);
            } catch (ElementNotFoundException e) {
                throw new IllegalArgumentException("Resource type '" + blueprint.getResourceTypePath() + "' not found" +
                        " when resolved to '" + resourceTypePath + "' while trying to wire up new resource on path '" +
                        parentPath.extend(SegmentType.r, blueprint.getId()) + "'.");
            }

            //specifically do NOT check relationship rules, here because defines cannot be created "manually".
            //here we "know what we are doing" and need to create the defines relationship to capture the
            //contract of the resource.
            BE r = tx.relate(resourceTypeObject, entity, defines.name(), null);

            CanonicalPath entityPath = tx.extractCanonicalPath(entity);
            resourceTypePath = tx.extractCanonicalPath(resourceTypeObject);

            ResourceType resourceType = tx.convert(resourceTypeObject, ResourceType.class);

            Resource ret = new Resource(blueprint.getName(), parentPath.extend(Resource.SEGMENT_TYPE,
                    tx.extractId(entity)).get(), null, null, null, resourceType, blueprint.getProperties());

            Relationship definesRel = new Relationship(tx.extractId(r), defines.name(), resourceTypePath,
                    entityPath);

            List<Notification<?, ?>> notifications = new ArrayList<>();
            notifications.add(new Notification<>(definesRel, definesRel, created()));

            if (tx.extractType(parent).equals(Resource.class)) {
                //we're creating a child resource... need to also create the implicit isParentOf
                //in here, we do use the relationship rules to check if the hierarchy we're introducing by this call
                //conforms to the rules.
                String relationshipName = isParentOf.name();
                RelationshipRules.checkCreate(tx, parent, outgoing, relationshipName, entity);
                r = tx.relate(parent, entity, relationshipName, null);<commit_msg>The error message was constructed using the Path.Extender instance instead of a fully constructed CanonicalPath. The Extender doesn't override toString() so the error message was kinda useless.<commit_after>/*
 * Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * 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 org.hawkular.inventory.base;

import static org.hawkular.inventory.api.Action.created;
import static org.hawkular.inventory.api.Relationships.Direction.outgoing;
import static org.hawkular.inventory.api.Relationships.WellKnown.contains;
import static org.hawkular.inventory.api.Relationships.WellKnown.defines;
import static org.hawkular.inventory.api.Relationships.WellKnown.incorporates;
import static org.hawkular.inventory.api.Relationships.WellKnown.isParentOf;
import static org.hawkular.inventory.api.filters.With.id;

import java.util.ArrayList;
import java.util.List;

import org.hawkular.inventory.api.Data;
import org.hawkular.inventory.api.EntityAlreadyExistsException;
import org.hawkular.inventory.api.EntityNotFoundException;
import org.hawkular.inventory.api.Metrics;
import org.hawkular.inventory.api.RelationAlreadyExistsException;
import org.hawkular.inventory.api.RelationNotFoundException;
import org.hawkular.inventory.api.Resources;
import org.hawkular.inventory.api.filters.Filter;
import org.hawkular.inventory.api.filters.RecurseFilter;
import org.hawkular.inventory.api.filters.Related;
import org.hawkular.inventory.api.filters.With;
import org.hawkular.inventory.api.model.DataEntity;
import org.hawkular.inventory.api.model.Metric;
import org.hawkular.inventory.api.model.Relationship;
import org.hawkular.inventory.api.model.Resource;
import org.hawkular.inventory.api.model.ResourceType;
import org.hawkular.inventory.base.spi.ElementNotFoundException;
import org.hawkular.inventory.paths.CanonicalPath;
import org.hawkular.inventory.paths.DataRole;
import org.hawkular.inventory.paths.Path;
import org.hawkular.inventory.paths.SegmentType;

/**
 * @author <NAME>ci
 * @since 0.1.0
 */
public final class BaseResources {

    private BaseResources() {

    }

    public static class ReadWrite<BE> extends Mutator<BE, Resource, Resource.Blueprint, Resource.Update, String>
            implements Resources.ReadWrite {

        public ReadWrite(TraversalContext<BE, Resource> context) {
            super(context);
        }

        @Override
        protected String getProposedId(Transaction<BE> tx, Resource.Blueprint blueprint) {
            return blueprint.getId();
        }

        @Override
        protected EntityAndPendingNotifications<BE, Resource> wireUpNewEntity(BE entity,
                                                                              Resource.Blueprint blueprint,
                                                                              CanonicalPath parentPath, BE parent,
                                                                              Transaction<BE> tx) {

            BE resourceTypeObject;
            CanonicalPath resourceTypePath = null;
            try {
                CanonicalPath tenant = CanonicalPath.of().tenant(parentPath.ids().getTenantId()).get();
                resourceTypePath = Util.canonicalize(blueprint.getResourceTypePath(), tenant,
                        parentPath, ResourceType.SEGMENT_TYPE);
                resourceTypeObject = tx.find(resourceTypePath);
            } catch (ElementNotFoundException e) {
                throw new IllegalArgumentException("Resource type '" + blueprint.getResourceTypePath() + "' not found" +
                        " when resolved to '" + resourceTypePath + "' while trying to wire up a new resource on path '"
                        + parentPath.extend(SegmentType.r, blueprint.getId()).get() + "'.");
            }

            //specifically do NOT check relationship rules, here because defines cannot be created "manually".
            //here we "know what we are doing" and need to create the defines relationship to capture the
            //contract of the resource.
            BE r = tx.relate(resourceTypeObject, entity, defines.name(), null);

            CanonicalPath entityPath = tx.extractCanonicalPath(entity);
            resourceTypePath = tx.extractCanonicalPath(resourceTypeObject);

            ResourceType resourceType = tx.convert(resourceTypeObject, ResourceType.class);

            Resource ret = new Resource(blueprint.getName(), parentPath.extend(Resource.SEGMENT_TYPE,
                    tx.extractId(entity)).get(), null, null, null, resourceType, blueprint.getProperties());

            Relationship definesRel = new Relationship(tx.extractId(r), defines.name(), resourceTypePath,
                    entityPath);

            List<Notification<?, ?>> notifications = new ArrayList<>();
            notifications.add(new Notification<>(definesRel, definesRel, created()));

            if (tx.extractType(parent).equals(Resource.class)) {
                //we're creating a child resource... need to also create the implicit isParentOf
                //in here, we do use the relationship rules to check if the hierarchy we're introducing by this call
                //conforms to the rules.
                String relationshipName = isParentOf.name();
                RelationshipRules.checkCreate(tx, parent, outgoing, relationshipName, entity);
                r = tx.relate(parent, entity, relationshipName, null);
{'fraction_non_alphanumeric': 0.08899594320486816, 'fraction_numerical': 0.002535496957403651, 'mean_word_length': 2.8619451697127936, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6276014', 'n_tokens_mistral': 3053, 'n_tokens_neox': 2942, 'n_words': 788}