summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Offer_Modifier.cpp
blob: d1821138a87d713d27d195ea37ff447deb83cbc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// $Id$

#include "Offer_Modifier.h"

TAO_Offer_Modifier::
TAO_Offer_Modifier (const char* type_name,
		    CosTradingRepos::ServiceTypeRepository::TypeStruct* type_struct,
		    CosTrading::Offer& offer)
  : offer_ (offer),
    type_ (type_name)
{
  CosTrading::PropertySeq& prop_seq = this->offer_.properties;
  CosTradingRepos::ServiceTypeRepository::PropStructSeq&
    pstructs = type_struct->props;
  int pstructs_length = pstructs.length (),
    props_length = prop_seq.length ();

  // Separate the type defined properties into mandatory and readonly
  for (int i = 0; i < pstructs_length; i++)
    {
      if (pstructs[i].mode ==
	  CosTradingRepos::ServiceTypeRepository::PROP_MANDATORY)
	{
	  TAO_String_Hash_Key prop_name ((const char *) pstructs[i].name);
	  this->mandatory_.insert (prop_name);
	}
      else if (pstructs[i].mode ==
	       CosTradingRepos::ServiceTypeRepository::PROP_READONLY)
	{
	  TAO_String_Hash_Key prop_name ((const char *) pstructs[i].name);
	  this->readonly_.insert (prop_name);
	}
    }

  // Insert the properties of the offer into a map.
  for (i = 0; i < props_length; i++)
    {
      TAO_String_Hash_Key prop_name = (const char*) prop_seq[i].name;
      this->props_.bind (prop_name, &prop_seq[i]);
    }
}

void
TAO_Offer_Modifier::
delete_properties (const CosTrading::PropertyNameSeq& deletes,
		   CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::Register::UnknownPropertyName, 
		  CosTrading::Register::MandatoryProperty,
		  CosTrading::IllegalPropertyName,
		  CosTrading::DuplicatePropertyName))
{
  Prop_Names delete_me;
  // Validate that the listed property names can be deleted
  for (int i = 0, length = deletes.length (); i < length; i++)
    {
      const char* dname = (const char *) deletes[i];
      if (! TAO_Trader_Base::is_valid_identifier_name (dname))
	TAO_THROW (CosTrading::IllegalPropertyName (dname));
      else
	{
	  TAO_String_Hash_Key prop_name (dname);
	  if (this->mandatory_.find (prop_name) == 0)
	    TAO_THROW (CosTrading::Register::MandatoryProperty (this->type_, dname));
	  else if (delete_me.insert (prop_name) == 1)
	    TAO_THROW (CosTrading::DuplicatePropertyName (dname));
	  else if (this->props_.find (prop_name) == 0)
	    TAO_THROW (CosTrading::Register::UnknownPropertyName (dname));
	}
    }

  // Delete those properties from the offer.
  for (i = 0; i < length; i++)
    {
      TAO_String_Hash_Key prop_name = (const char *) deletes[i];
      this->props_.unbind (prop_name);
    } 
}

void
TAO_Offer_Modifier::
merge_properties (const CosTrading::PropertySeq& modifies,
		  CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::IllegalPropertyName,
		   CosTrading::DuplicatePropertyName,
		   CosTrading::Register::ReadonlyProperty))
{
  Prop_Names modify_me;
  // Ensure that the proposed changes aren't to readonly properties or
  // otherwise invalid.
  for (int i = 0, length = modifies.length (); i < length; i++)
    {
      const char*  mname =(const char *) modifies[i].name;
      if (! TAO_Trader_Base::is_valid_identifier_name (mname))
	TAO_THROW (CosTrading::IllegalPropertyName (mname));
      else
	{
	  TAO_String_Hash_Key prop_name (mname);
	  if (this->readonly_.find (prop_name) == 0 &&
	      this->props_.find (prop_name) == 0)
	    TAO_THROW (CosTrading::Register::ReadonlyProperty (this->type_, mname));
	  else if (modify_me.insert (prop_name) == 1)
	    TAO_THROW (CosTrading::DuplicatePropertyName (mname));
	}
    }

  for (i = 0; i < length; i++)
    {
      // Add a property to the destination if it doesn't already exist.
      Props::ENTRY* existing_entry = 0;
      TAO_String_Hash_Key prop_name ((const char*) modifies[i].name);

      if (this->props_.bind (prop_name,
			     (CosTrading::Property *) &modifies[i],
			     existing_entry) == 1)
	{
	  // Modify a property if it already exists in the destination.
	  CosTrading::Property* prop = existing_entry->int_id_;	  
	  prop->value = modifies[i].value;
	}
    }
}

CosTrading::Offer&
TAO_Offer_Modifier::affect_change (void)
{
  int elem = 0;
  CosTrading::PropertySeq prop_seq;

  // Create a new property list reflecting the deletes, modifies, and
  // add operations performed, and place this property list in the
  // offer. 
  prop_seq.length (this->props_.current_size ());
  for (Props::iterator props_iter (this->props_);
       ! props_iter.done ();
       props_iter++, elem++)
    {
      prop_seq[elem] = *(*props_iter).int_id_;
    }

  this->offer_.properties = prop_seq;
  return this->offer_;
}