from the relationship subject, determine which datastream to modify etc
545255
private String resolveSubjectToDatastream(String subject) throws ServerException{
        String pidURI = PID.toURI(m_obj.getPid());
        if (subject.startsWith(pidURI)) {
            if (subject.length() == pidURI.length()) {
                return "RELS-EXT";
            }
            if (subject.charAt(pidURI.length()) == '/') {
                return "RELS-INT";
            }
        }
        throw new GeneralException("Cannot determine which relationship datastream to update for subject " + subject + ".  Relationship subjects must be the URI of the object or the URI of a datastream within the object.");
    }
1202735
@SuppressWarnings("deprecation")
    public static void updateLegacyDatastreams(DigitalObject obj) {
        final String xml = "text/xml";
        final String rdf = "application/rdf+xml";
        updateLegacyDatastream(obj, "DC", xml, OAI_DC2_0.uri);
        updateLegacyDatastream(obj, "RELS-EXT", rdf, RELS_EXT1_0.uri);
        updateLegacyDatastream(obj, "RELS-INT", rdf, RELS_INT1_0.uri);
        updateLegacyDatastream(obj, "POLICY", xml, XACML_POLICY1_0.uri);
        String fType = obj.getExtProperty(RDF.TYPE.uri);
        if (MODEL.BDEF_OBJECT.looselyMatches(fType, false)) {
            updateLegacyDatastream(obj,
                                   "METHODMAP",
                                   xml,
                                   SDEF_METHOD_MAP1_0.uri);
        } else if (MODEL.BMECH_OBJECT.looselyMatches(fType, false)) {
            updateLegacyDatastream(obj,
                                   "METHODMAP",
                                   xml,
                                   SDEP_METHOD_MAP1_1.uri);
            updateLegacyDatastream(obj,
                                   "DSINPUTSPEC",
                                   xml,
                                   DS_INPUT_SPEC1_1.uri);
            updateLegacyDatastream(obj,
                                   "WSDL",
                                   xml,
                                   WSDL.uri);
        }
    }
995274
private void onRelationalAttributes(List<RelationHolder> rlHolders, Row row, Table schemaTable)
    {
        // Iterate over relations
        if (rlHolders != null && !rlHolders.isEmpty())
        {
            for (RelationHolder rh : rlHolders)
            {
                String relationName = rh.getRelationName();
                Object valueObj = rh.getRelationValue();

                if (!StringUtils.isEmpty(relationName) && valueObj != null)
                {
                    if (valueObj != null)
                    {
                        NoSqlDBUtils.add(schemaTable.getField(relationName), row, valueObj, relationName);
                        KunderaCoreUtils.printQuery(
                                "Add relation: relation name:" + relationName + "relation value:" + valueObj,
                                showQuery);
                    }
                }
            }
        }
    }
491170
public void addDatastream(Datastream datastream, boolean addNewVersion)
            throws ServerException {
        assertNotInvalidated();
        assertNotPendingRemoval();
        // use this call to handle versionable
        m_obj.addDatastreamVersion(datastream, addNewVersion);
    }
128098
protected void processRelationship(final ParserData parserData, final SpecNodeWithRelationships specNode,
            final Relationship relationship) {
        final String relatedId = relationship.getSecondaryRelationshipId();
        // The relationship points to a target so it must be a level or topic
        if (relatedId.toUpperCase(Locale.ENGLISH).matches(ProcessorConstants.TARGET_REGEX)) {
            if (parserData.getTargetTopics().containsKey(relatedId) && !parserData.getTargetLevels().containsKey(relatedId)) {
                specNode.addRelationshipToTarget(parserData.getTargetTopics().get(relatedId), relationship.getType(),
                        relationship.getRelationshipTitle());
            } else if (!parserData.getTargetTopics().containsKey(relatedId) && parserData.getTargetLevels().containsKey(relatedId)) {
                specNode.addRelationshipToTarget(parserData.getTargetLevels().get(relatedId), relationship.getType(),
                        relationship.getRelationshipTitle());
            } else {
                final SpecTopic dummyTopic = new SpecTopic(-1, "");
                dummyTopic.setTargetId(relatedId);
                dummyTopic.setUniqueId("-1");
                specNode.addRelationshipToTarget(dummyTopic, relationship.getType(), relationship.getRelationshipTitle());
            }
        }
        // The relationship isn't a target so it must point to a topic directly
        else {
            if (!CSConstants.NEW_TOPIC_ID_PATTERN.matcher(relatedId).matches()) {
                // The relationship isn't a unique new topic so it will contain the line number in front of
                // the topic ID
                if (!relatedId.startsWith("X")) {
                    int count = 0;
                    SpecTopic relatedTopic = null;

                    // Get the related topic and count if more then one is found
                    for (final Map.Entry<String, SpecTopic> specTopicEntry : parserData.getSpecTopics().entrySet()) {
                        if (specTopicEntry.getKey().matches("^[\\w\\d]+-" + relatedId + "$")) {
                            relatedTopic = specTopicEntry.getValue();
                            count++;
                        }
                    }

                    /*
                     * Add the relationship to the node even if the relationship isn't duplicated
                     * and the related topic isn't the current topic. This is so it shows up in the
                     * output.
                     */
                    if (count > 0) {
                        specNode.addRelationshipToTopic(relatedTopic, relationship.getType(), relationship.getRelationshipTitle());
                    } else {
                        final SpecTopic dummyTopic = new SpecTopic(-1, "");
                        dummyTopic.setId(relatedId);
                        dummyTopic.setUniqueId("-1");
                        specNode.addRelationshipToTopic(dummyTopic, relationship.getType(), relationship.getRelationshipTitle());
                    }
                } else {
                    final SpecTopic dummyTopic = new SpecTopic(-1, "");
                    dummyTopic.setId(relatedId);
                    dummyTopic.setUniqueId("-1");
                    specNode.addRelationshipToTopic(dummyTopic, relationship.getType(), relationship.getRelationshipTitle());
                }
            } else {
                if (parserData.getSpecTopics().containsKey(relatedId)) {
                    final SpecTopic relatedSpecTopic = parserData.getSpecTopics().get(relatedId);

                    // Check that a duplicate doesn't exist, because if it does the new topic isn't unique
                    String duplicatedId = "X" + relatedId.substring(1);
                    boolean duplicateExists = false;
                    for (String uniqueTopicId : parserData.getSpecTopics().keySet()) {
                        if (uniqueTopicId.matches("^[\\w\\d]+-" + duplicatedId + "$")) {
                            duplicateExists = true;
                            break;
                        }
                    }

                    if (relatedSpecTopic != specNode) {
                        if (!duplicateExists) {
                            specNode.addRelationshipToTopic(relatedSpecTopic, relationship.getType(), relationship.getRelationshipTitle());
                        } else {
                            // Only create a new target if one doesn't already exist
                            if (relatedSpecTopic.getTargetId() == null) {
                                String targetId = ContentSpecUtilities.generateRandomTargetId(relatedSpecTopic.getUniqueId());
                                while (parserData.getTargetTopics().containsKey(targetId) || parserData.getTargetLevels().containsKey(
                                        targetId)) {
                                    targetId = ContentSpecUtilities.generateRandomTargetId(relatedSpecTopic.getUniqueId());
                                }
                                parserData.getSpecTopics().get(relatedId).setTargetId(targetId);
                                parserData.getTargetTopics().put(targetId, relatedSpecTopic);
                            }
                            specNode.addRelationshipToTopic(relatedSpecTopic, relationship.getType(), relationship.getRelationshipTitle());
                        }
                    }
                } else {
                    final SpecTopic dummyTopic = new SpecTopic(-1, "");
                    dummyTopic.setId(relatedId);
                    dummyTopic.setUniqueId("-1");
                    specNode.addRelationshipToTopic(dummyTopic, relationship.getType(), relationship.getRelationshipTitle());
                }
            }
        }
    }
224230
def _ReferenceFromPairs(pairs, reference=None, app=None, namespace=None):
  
  if reference is None:
    reference = entity_pb.Reference()
  path = reference.mutable_path()
  last = False
  for kind, idorname in pairs:
    if last:
      raise datastore_errors.BadArgumentError(
          'Incomplete Key entry must be last')
    t = type(kind)
    if t is str:
      pass
    elif t is unicode:
      kind = kind.encode('utf8')
    else:
      if issubclass(t, type):
        # Late import to avoid cycles.
        from .model import Model
        modelclass = kind
        if not issubclass(modelclass, Model):
          raise TypeError('Key kind must be either a string or subclass of '
                          'Model; received %r' % modelclass)
        kind = modelclass._get_kind()
        t = type(kind)
      if t is str:
        pass
      elif t is unicode:
        kind = kind.encode('utf8')
      elif issubclass(t, str):
        pass
      elif issubclass(t, unicode):
        kind = kind.encode('utf8')
      else:
        raise TypeError('Key kind must be either a string or subclass of Model;'
                        ' received %r' % kind)
    # pylint: disable=superfluous-parens
    if not (1 <= len(kind) <= _MAX_KEYPART_BYTES):
      raise ValueError('Key kind string must be a non-empty string up to %i'
                       'bytes; received %s' %
                       (_MAX_KEYPART_BYTES, kind))
    elem = path.add_element()
    elem.set_type(kind)
    t = type(idorname)
    if t is int or t is long:
      # pylint: disable=superfluous-parens
      if not (1 <= idorname < _MAX_LONG):
        raise ValueError('Key id number is too long; received %i' % idorname)
      elem.set_id(idorname)
    elif t is str:
      # pylint: disable=superfluous-parens
      if not (1 <= len(idorname) <= _MAX_KEYPART_BYTES):
        raise ValueError('Key name strings must be non-empty strings up to %i '
                         'bytes; received %s' %
                         (_MAX_KEYPART_BYTES, idorname))
      elem.set_name(idorname)
    elif t is unicode:
      idorname = idorname.encode('utf8')
      # pylint: disable=superfluous-parens
      if not (1 <= len(idorname) <= _MAX_KEYPART_BYTES):
        raise ValueError('Key name unicode strings must be non-empty strings up'
                         ' to %i bytes; received %s' %
                         (_MAX_KEYPART_BYTES, idorname))
      elem.set_name(idorname)
    elif idorname is None:
      last = True
    elif issubclass(t, (int, long)):
      # pylint: disable=superfluous-parens
      if not (1 <= idorname < _MAX_LONG):
        raise ValueError('Key id number is too long; received %i' % idorname)
      elem.set_id(idorname)
    elif issubclass(t, basestring):
      if issubclass(t, unicode):
        idorname = idorname.encode('utf8')
      # pylint: disable=superfluous-parens
      if not (1 <= len(idorname) <= _MAX_KEYPART_BYTES):
        raise ValueError('Key name strings must be non-empty strings up to %i '
                         'bytes; received %s' % (_MAX_KEYPART_BYTES, idorname))
      elem.set_name(idorname)
    else:
      raise TypeError('id must be either a numeric id or a string name; '
                      'received %r' % idorname)
  # An empty app id means to use the default app id.
  if not app:
    app = _DefaultAppId()
  # Always set the app id, since it is mandatory.
  reference.set_app(app)
  # An empty namespace overrides the default namespace.
  if namespace is None:
    namespace = _DefaultNamespace()
  # Only set the namespace if it is not empty.
  if namespace:
    reference.set_name_space(namespace)
  return reference
540768
private void encodeSubject(Subject subject, PrintStream out,
                                  Indenter indenter) {
        char [] indent = indenter.makeString().toCharArray();
        out.print(indent);
        out.append("<Subject SubjectCategory=\"")
           .append(subject.getCategory().toString()).append('"');

        List subjectAttrs = subject.getAttributesAsList();
        
        if (subjectAttrs.size() == 0) {
            // there's nothing in this Subject, so just close the tag
            out.println("/>");
        } else {
            // there's content, so fill it in
            out.println('>');

            encodeAttributes(subjectAttrs, out, indenter);
        
            out.print(indent);
            out.println("</Subject>");
        }
    }
204654
private void populateRelation(TaskField field, Task sourceTask, String relationship) throws MPXJException
   {
      int index = 0;
      int length = relationship.length();

      //
      // Extract the identifier
      //
      while ((index < length) && (Character.isDigit(relationship.charAt(index)) == true))
      {
         ++index;
      }

      Integer taskID;
      try
      {
         taskID = Integer.valueOf(relationship.substring(0, index));
      }

      catch (NumberFormatException ex)
      {
         throw new MPXJException(MPXJException.INVALID_FORMAT + " '" + relationship + "'");
      }

      //
      // Now find the task, so we can extract the unique ID
      //
      Task targetTask;
      if (field == TaskField.PREDECESSORS)
      {
         targetTask = m_projectFile.getTaskByID(taskID);
      }
      else
      {
         targetTask = m_projectFile.getTaskByUniqueID(taskID);
      }

      //
      // If we haven't reached the end, we next expect to find
      // SF, SS, FS, FF
      //
      RelationType type = null;
      Duration lag = null;

      if (index == length)
      {
         type = RelationType.FINISH_START;
         lag = Duration.getInstance(0, TimeUnit.DAYS);
      }
      else
      {
         if ((index + 1) == length)
         {
            throw new MPXJException(MPXJException.INVALID_FORMAT + " '" + relationship + "'");
         }

         type = RelationTypeUtility.getInstance(m_locale, relationship.substring(index, index + 2));

         index += 2;

         if (index == length)
         {
            lag = Duration.getInstance(0, TimeUnit.DAYS);
         }
         else
         {
            if (relationship.charAt(index) == '+')
            {
               ++index;
            }

            lag = DurationUtility.getInstance(relationship.substring(index), m_formats.getDurationDecimalFormat(), m_locale);
         }
      }

      if (type == null)
      {
         throw new MPXJException(MPXJException.INVALID_FORMAT + " '" + relationship + "'");
      }

      // We have seen at least one example MPX file where an invalid task ID
      // is present. We'll ignore this as the schedule is otherwise valid.
      if (targetTask != null)
      {
         Relation relation = sourceTask.addPredecessor(targetTask, type, lag);
         m_eventManager.fireRelationReadEvent(relation);
      }
   }
183611
private void addDCTriples(Datastream ds,
                              URIReference objURI,
                              Set<Triple> set) throws Exception {
        DCFields dc = new DCFields(ds.getContentStream());
        Map<RDFName, List<DCField>> map = dc.getMap();
        for (RDFName predicate : map.keySet()) {
            for (DCField dcField : map.get(predicate)) {
                String lang = dcField.getLang();
                if (lang == null) {
                    add(objURI, predicate, dcField.getValue(), set);
                } else {
                    add(objURI, predicate, dcField.getValue(), lang, set);
                }
            }
        }
    }
414346
function(data, payload) {
        if (data.relationships) {
          this.extractRelationships(data.relationships, data);
        }
        payload[data.type] = data;
        delete data.type;
      }
1186994
@Override
   public void newEmbeddedRelationship(
            final Project project,
            final JavaResource resource,
            final String fieldName,
            final String fieldType) throws FileNotFoundException
   {
      JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
      JavaClassSource entityClass = getJavaClassFrom(resource);
      JavaClassSource fieldEntityClass;
      if (areTypesSame(fieldType, entityClass.getCanonicalName()))
      {
         fieldEntityClass = entityClass;
      }
      else
      {
         fieldEntityClass = findEntity(project, fieldType);
         entityClass.addImport(fieldEntityClass);
      }

      addFieldTo(entityClass, fieldEntityClass.getName(), fieldName,
               Embedded.class.getName());
      java.saveJavaSource(entityClass);
   }
1244573
private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException  {
        // Read in any hidden stuff
        s.defaultReadObject();

        // Read in size (number of Mappings)
        int size = s.readInt();
        if (size < 0)
            throw new java.io.StreamCorruptedException
                ("Illegal mappings count: " + size);
        init(capacity(size));

        // Read the keys and values, and put the mappings in the table
        for (int i=0; i<size; i++) {
            @SuppressWarnings("unchecked")
                K key = (K) s.readObject();
            @SuppressWarnings("unchecked")
                V value = (V) s.readObject();
            putForCreate(key, value);
        }
    }
170471
def _get_datastreams(self):
        
        if self._create:
            # FIXME: should we default to the datastreams defined in code?
            return {}
        else:
            # NOTE: can be accessed as a cached class property via ds_list
            r = self.api.listDatastreams(self.pid)
            dsobj = parse_xml_object(ObjectDatastreams, r.content, r.url)
            return dict([(ds.dsid, ds) for ds in dsobj.datastreams])
743655
private Stream<Type> scopeTypes(GraqlCompute query) {
        // Get all types if query.inTypes() is empty, else get all scoped types of each meta type.
        // Only include attributes and implicit "has-xxx" relations when user specifically asked for them.
        if (query.in().isEmpty()) {
            ImmutableSet.Builder<Type> typeBuilder = ImmutableSet.builder();

            if (scopeIncludesAttributes(query)) {
                tx.getMetaConcept().subs().forEach(typeBuilder::add);
            } else {
                tx.getMetaEntityType().subs().forEach(typeBuilder::add);
                tx.getMetaRelationType().subs()
                        .filter(relationType -> !relationType.isImplicit()).forEach(typeBuilder::add);
            }

            return typeBuilder.build().stream();
        } else {
            Stream<Type> subTypes = query.in().stream().map(t -> {
                Label label = Label.of(t);
                Type type = tx.getType(label);
                if (type == null) throw GraqlQueryException.labelNotFound(label);
                return type;
            }).flatMap(Type::subs);

            if (!scopeIncludesAttributes(query)) {
                subTypes = subTypes.filter(relationType -> !relationType.isImplicit());
            }

            return subTypes;
        }
    }
920321
function resolveRelations(attrNode, context) {
    if (attrNode.dataSources || attrNode.resource) {
        const parentResource = context.parentResource;
        let childResource = attrNode;

        context.errorContext = getErrorContext(!parentResource ? 'resource' : 'sub-resource', context);

        if (attrNode.resource) {
            if (!context.resourceConfigs[attrNode.resource]) {
                throw new ImplementationError('Unknown resource "' + attrNode.resource + '"' + context.errorContext);
            }
            childResource = context.resourceConfigs[attrNode.resource].config;
        }

        if (parentResource) {
            if (!('parentKey' in attrNode)) throw new ImplementationError('Missing parentKey' + context.errorContext);
            if (!('childKey' in attrNode)) throw new ImplementationError('Missing childKey' + context.errorContext);

            if (attrNode.parentKey === null) {
                attrNode.parentKey = _.cloneDeep(parentResource.primaryKey);
            }
            if (attrNode.childKey === null) {
                attrNode.childKey = _.cloneDeep(childResource.primaryKey);
            }

            if (attrNode.joinVia) {
                const joinViaDataSource = attrNode.dataSources[attrNode.joinVia];
                if (attrNode.parentKey.length !== joinViaDataSource.joinParentKey.length) {
                    throw new ImplementationError(
                        'Composite key length of parentKey (' +
                            attrNode.parentKey.length +
                            ') does not match joinParentKey length (' +
                            joinViaDataSource.joinParentKey.length +
                            ') ' +
                            'of DataSource "' +
                            attrNode.joinVia +
                            '"' +
                            context.errorContext
                    );
                }
                if (attrNode.childKey.length !== joinViaDataSource.joinChildKey.length) {
                    throw new ImplementationError(
                        'Composite key length of childKey (' +
                            attrNode.childKey.length +
                            ') does not match joinChildKey length (' +
                            joinViaDataSource.joinChildKey.length +
                            ') ' +
                            'of DataSource "' +
                            attrNode.joinVia +
                            '"' +
                            context.errorContext
                    );
                }
            } else if (attrNode.parentKey.length !== attrNode.childKey.length) {
                throw new ImplementationError(
                    'Composite key length of parentKey (' +
                        attrNode.parentKey.length +
                        ') does not match childKey length (' +
                        attrNode.childKey.length +
                        ')' +
                        context.errorContext
                );
            }

            const errorContext = context.errorContext;
            context.errorContext = ' in parentKey' + errorContext;
            attrNode.resolvedParentKey = resolveKey(
                attrNode.parentKey,
                parentResource,
                {
                    allowMultiValued: true
                },
                context
            );

            context.errorContext = ' in childKey' + errorContext;
            attrNode.resolvedChildKey = resolveKey(
                attrNode.childKey,
                childResource,
                {
                    neededDataSources: ['primary'],
                    allowMultiValued: false
                },
                context
            );
            context.errorContext = errorContext;
        }

        context.parentResource = attrNode;
    }

    if (attrNode.attributes) {
        Object.keys(attrNode.attributes).forEach(subAttrName => {
            const subAttrNode = attrNode.attributes[subAttrName];
            const subContext = Object.assign({}, context);
            subContext.attrPath = context.attrPath.concat([subAttrName]);

            resolveRelations(subAttrNode, subContext);
        });
    }
}
230827
def update_dependency_kinds(apps, schema_editor):
    """"""
    DataDependency = apps.get_model('flow', 'DataDependency')
    for dependency in DataDependency.objects.all():
        # Assume dependency is of subprocess kind.
        dependency.kind = 'subprocess'

        # Check child inputs to determine if this is an IO dependency.
        child = dependency.child
        parent = dependency.parent

        for field_schema, fields in iterate_fields(child.input, child.process.input_schema):
            name = field_schema['name']
            value = fields[name]

            if field_schema.get('type', '').startswith('data:'):
                if value == parent.pk:
                    dependency.kind = 'io'
                    break
            elif field_schema.get('type', '').startswith('list:data:'):
                for data in value:
                    if value == parent.pk:
                        dependency.kind = 'io'
                        break

        dependency.save()
480512
private static String adjustData(String data, ClassNode type, DelegationMetadata dmd) {
        StringBuilder path = new StringBuilder();
        int i = 0;
        String[] propertyPath = data.split("\\.");
        while (dmd != null) {
            int strategy = dmd.getStrategy();
            ClassNode delegate = dmd.getType();
            dmd = dmd.getParent();
            switch (strategy) {
                case Closure.DELEGATE_FIRST:
                    if (!delegate.isDerivedFrom(CLOSURE_TYPE) && !delegate.isDerivedFrom(type)) {
                        path.append("owner"); // must be non-delegate case
                    } else {
                        path.append("delegate");
                    }
                    break;
                default:
                    if (i >= propertyPath.length) return data;
                    path.append(propertyPath[i]);
            }
            if (type.equals(delegate)) break;
            i++;
            if (dmd != null) path.append('.');
        }
        String result = path.toString();
        if (!result.isEmpty()) {
            return result;
        }
        return data;
    }