00001
00002
00003 #include "StringUtil.h"
00004 #include "YosokumoProtobuf.h"
00005 #include "EmptyValue.h"
00006 #include "IntegerValue.h"
00007 #include "NaturalValue.h"
00008 #include "RealValue.h"
00009 #include "SpecialValue.h"
00010
00011 #include "EmptyBlock.h"
00012 #include "CellBlock.h"
00013 #include "PredictorBlock.h"
00014 #include "SpecimenBlock.h"
00015
00016
00017 using namespace Yosokumo;
00018
00019
00020 std::string YosokumoProtobuf::getContentType()
00021 {
00022 return "application/yosokumo+protobuf";
00023 }
00024
00025
00026
00027 bool YosokumoProtobuf::makeCatalogFromBytes(
00028 const std::vector<uint8_t> &catalogAsBytes,
00029 Catalog &catalog)
00030 {
00031 ProtoBuf::Catalog protoCatalog;
00032
00033 if (!makeProtobufCatalogFromBytes(catalogAsBytes, protoCatalog))
00034 return false;
00035
00036 return makeCatalogFromProtobufCatalog(protoCatalog, catalog);
00037 }
00038
00039 bool YosokumoProtobuf::makeProtobufCatalogFromBytes(
00040 const std::vector<uint8_t> &catalogAsBytes,
00041 ProtoBuf::Catalog &protoCatalog)
00042 {
00043 int n = int(catalogAsBytes.size());
00044
00045 if (n == 0)
00046 {
00047 exception = ServiceException(
00048 "input vector of bytes is empty",
00049 "makeProtobufCatalogFromBytes");
00050 return false;
00051 }
00052
00053 if (!protoCatalog.ParseFromArray(&catalogAsBytes[0], n))
00054 {
00055 exception = ServiceException(
00056 "ProtoBuf::Catalog::ParseFromArray failed",
00057 "makeProtobufCatalogFromBytes");
00058 return false;
00059 }
00060
00061 return true;
00062
00063 }
00064
00065
00066 bool YosokumoProtobuf::makeCatalogFromProtobufCatalog(
00067 const ProtoBuf::Catalog &protoCatalog,
00068 Catalog &catalog)
00069 {
00070
00071
00072 catalog.setUserIdentifier (protoCatalog.user_identifier());
00073 catalog.setUserName (protoCatalog.user_name() );
00074 catalog.setCatalogLocation(protoCatalog.location() );
00075
00076 catalog.clearStudies();
00077
00078 for (int i = 0; i < protoCatalog.study_size(); ++i)
00079 {
00080 const ProtoBuf::Study &protoStudy = protoCatalog.study(i);
00081
00082 Study study;
00083
00084 if (!makeStudyFromProtobufStudy(protoStudy, study))
00085 return false;
00086
00087 if (!catalog.addStudy(study))
00088 {
00089 exception = ServiceException(
00090 "catalog contains two studies with the same identifier",
00091 "makeCatalogFromProtobufCatalog");
00092
00093 return false;
00094 }
00095 }
00096
00097 return true;
00098
00099 }
00100
00101
00102
00103 bool YosokumoProtobuf::makeBytesFromCatalog(
00104 const Catalog &catalog,
00105 std::vector<uint8_t> &catalogAsBytes)
00106 {
00107 ProtoBuf::Catalog protoCatalog;
00108
00109 if (!makeProtobufCatalogFromCatalog(catalog, protoCatalog))
00110 return false;
00111
00112 return makeBytesFromProtobufCatalog(protoCatalog, catalogAsBytes);
00113 }
00114
00115
00116 bool YosokumoProtobuf::makeProtobufCatalogFromCatalog(
00117 const Catalog &catalog,
00118 ProtoBuf::Catalog &protoCatalog)
00119 {
00120 protoCatalog.set_user_identifier (catalog.getUserIdentifier());
00121 protoCatalog.set_user_name (catalog.getUserName());
00122 protoCatalog.set_location (catalog.getCatalogLocation());
00123
00124 Catalog::StudyConstIterator iter;
00125
00126 for (iter = catalog.begin(); iter != catalog.end(); ++iter)
00127 {
00128 const Study &study = iter->second;
00129
00130 ProtoBuf::Study *pProtoStudy = protoCatalog.add_study();
00131
00132 if (!makeProtobufStudyFromStudy(study, *pProtoStudy))
00133 return false;
00134 }
00135
00136 return true;
00137 }
00138
00139 bool YosokumoProtobuf::makeBytesFromProtobufCatalog(
00140 const ProtoBuf::Catalog &protoCatalog,
00141 std::vector<uint8_t> &catalogAsBytes)
00142 {
00143 int numBytes = protoCatalog.ByteSize();
00144
00145 catalogAsBytes.assign(numBytes, 0);
00146
00147 return protoCatalog.SerializeToArray(&catalogAsBytes[0], numBytes);
00148 }
00149
00150
00151
00152
00153 bool YosokumoProtobuf::makeStudyFromBytes(
00154 const std::vector<uint8_t> &studyAsBytes,
00155 Study &study)
00156 {
00157 ProtoBuf::Study protoStudy;
00158
00159 if (!makeProtobufStudyFromBytes(studyAsBytes, protoStudy))
00160 return false;
00161
00162 return makeStudyFromProtobufStudy(protoStudy, study);
00163
00164 }
00165
00166 bool YosokumoProtobuf::makeProtobufStudyFromBytes(
00167 const std::vector<uint8_t> &studyAsBytes,
00168 ProtoBuf::Study &protoStudy)
00169 {
00170 int n = int(studyAsBytes.size());
00171
00172 if (n == 0)
00173 {
00174 exception = ServiceException(
00175 "input vector of bytes is empty",
00176 "makeProtobufStudyFromBytes");
00177 return false;
00178 }
00179
00180 if (!protoStudy.ParseFromArray(&studyAsBytes[0], n))
00181 {
00182 exception = ServiceException(
00183 "ProtoBuf::Study::ParseFromArray failed",
00184 "makeProtobufStudyFromBytes");
00185 return false;
00186 }
00187
00188 return true;
00189
00190 }
00191
00192
00193 bool YosokumoProtobuf::makeStudyFromProtobufStudy(
00194 const ProtoBuf::Study &protoStudy,
00195 Study &study)
00196 {
00197 study.setStudyIdentifier(protoStudy.study_identifier());
00198 study.setStudyName (protoStudy.study_name() );
00199 study.setStudyLocation (protoStudy.location() );
00200
00201 Study::Type type;
00202 if (!protoTypeToType(protoStudy.type(), type))
00203 return false;
00204 study.setType(type);
00205
00206 Study::Status status;
00207 if (!protoStatusToStatus(protoStudy.status(), status))
00208 return false;
00209 study.setStatus(status);
00210
00211 Study::Visibility visibility;
00212 if (!protoVisibilityToVisibility(protoStudy.visibility(), visibility))
00213 return false;
00214 study.setVisibility(visibility);
00215
00216 study.setOwnerIdentifier(protoStudy.owner().user_identifier());
00217 study.setOwnerName (protoStudy.owner().user_name());
00218 study.setTableLocation (protoStudy.table().location());
00219 study.setModelLocation (protoStudy.model().location());
00220 study.setPanelLocation (protoStudy.panel().location());
00221 study.setRosterLocation (protoStudy.roster().location());
00222
00223 return true;
00224
00225 }
00226
00227
00228
00229
00230 bool YosokumoProtobuf::makeBytesFromStudy(
00231 const Study &study,
00232 std::vector<uint8_t> &studyAsBytes)
00233 {
00234 ProtoBuf::Study protoStudy;
00235
00236 if (!makeProtobufStudyFromStudy(study, protoStudy))
00237 return false;
00238
00239 return makeBytesFromProtobufStudy(protoStudy, studyAsBytes);
00240 }
00241
00242
00243 bool YosokumoProtobuf::makeProtobufStudyFromStudy(
00244 const Study &study,
00245 ProtoBuf::Study &protoStudy)
00246 {
00247 protoStudy.set_study_identifier (study.getStudyIdentifier());
00248 protoStudy.set_study_name (study.getStudyName());
00249
00250 ProtoBuf::Study_Type type;
00251 if (!typeToProtobufType(study.getType(), type))
00252 return false;
00253 protoStudy.set_type(type);
00254
00255 ProtoBuf::Study_Status status;
00256 if (!statusToProtobufStatus(study.getStatus(), status))
00257 return false;
00258 protoStudy.set_status(status);
00259
00260 ProtoBuf::Study_Visibility visibility;
00261 if (!visibilityToProtobufVisibility(study.getVisibility(), visibility))
00262 return false;
00263 protoStudy.set_visibility(visibility);
00264
00265 return true;
00266 }
00267
00268 bool YosokumoProtobuf::makeBytesFromProtobufStudy(
00269 const ProtoBuf::Study &protoStudy,
00270 std::vector<uint8_t> &studyAsBytes)
00271 {
00272 int numBytes = protoStudy.ByteSize();
00273
00274 studyAsBytes.assign(numBytes, 0);
00275
00276 return protoStudy.SerializeToArray(&studyAsBytes[0], numBytes);
00277 }
00278
00279
00280
00281 bool YosokumoProtobuf::makeStudyNameFromBytes(
00282 const std::vector<uint8_t> &studyNameAsBytes,
00283 std::string &name)
00284 {
00285 ProtoBuf::Panel_StudyNameControl protoNameControl;
00286
00287 if (!makeProtobufStudyNameControlFromBytes(
00288 studyNameAsBytes, protoNameControl))
00289 return false;
00290
00291 return makeStudyNameFromProtobufStudyNameControl(protoNameControl, name);
00292 }
00293
00294 bool YosokumoProtobuf::makeProtobufStudyNameControlFromBytes(
00295 const std::vector<uint8_t> &studyNameAsBytes,
00296 ProtoBuf::Panel_StudyNameControl &protoNameControl)
00297 {
00298 int numBytes = int(studyNameAsBytes.size());
00299
00300 if (numBytes == 0)
00301 {
00302 exception = ServiceException(
00303 "input vector of bytes is empty",
00304 "makeProtobufStudyNameControlFromBytes");
00305 return false;
00306 }
00307
00308 if (!protoNameControl.ParseFromArray(&studyNameAsBytes[0], numBytes))
00309 {
00310 exception = ServiceException(
00311 "ProtoBuf::Panel_StudyNameControl::ParseFromArray failed",
00312 "makeProtobufStudyNameControlFromBytes");
00313 return false;
00314 }
00315
00316 return true;
00317 }
00318
00319 bool YosokumoProtobuf::makeStudyNameFromProtobufStudyNameControl(
00320 const ProtoBuf::Panel_StudyNameControl &protoNameControl,
00321 std::string &name)
00322 {
00323 name = protoNameControl.study_name();
00324 return true;
00325 }
00326
00327
00328
00329 bool YosokumoProtobuf::makeBytesFromStudyName(
00330 const std::string &name,
00331 std::vector<uint8_t> &studyNameAsBytes)
00332 {
00333 ProtoBuf::Panel_StudyNameControl protoNameControl;
00334
00335 if (!makeProtobufStudyNameControlFromName(name, protoNameControl))
00336 return false;
00337
00338 return makeBytesFromProtobufStudyNameControl(protoNameControl,studyNameAsBytes);
00339 }
00340
00341 bool YosokumoProtobuf::makeProtobufStudyNameControlFromName(
00342 const std::string &name,
00343 ProtoBuf::Panel_StudyNameControl &protoNameControl)
00344 {
00345 protoNameControl.set_study_name(name);
00346 return true;
00347 }
00348
00349 bool YosokumoProtobuf::makeBytesFromProtobufStudyNameControl(
00350 const ProtoBuf::Panel_StudyNameControl &protoNameControl,
00351 std::vector<uint8_t> &studyNameAsBytes)
00352 {
00353 int numBytes = protoNameControl.ByteSize();
00354 studyNameAsBytes.assign(numBytes, 0);
00355 return protoNameControl.SerializeToArray(&studyNameAsBytes[0], numBytes);
00356 }
00357
00358
00359
00360 bool YosokumoProtobuf::makeStudyStatusFromBytes(
00361 const std::vector<uint8_t> &studyStatusAsBytes,
00362 Study::Status &status)
00363 {
00364 ProtoBuf::Panel_StatusControl protoStatusControl;
00365
00366 if (!makeProtobufStudyStatusControlFromBytes(
00367 studyStatusAsBytes, protoStatusControl))
00368 return false;
00369
00370 return makeStudyStatusFromProtobufStudyStatusControl(
00371 protoStatusControl, status);
00372 }
00373
00374 bool YosokumoProtobuf::makeProtobufStudyStatusControlFromBytes(
00375 const std::vector<uint8_t> &studyStatusAsBytes,
00376 ProtoBuf::Panel_StatusControl &protoStatusControl)
00377 {
00378 int numBytes = int(studyStatusAsBytes.size());
00379
00380 if (numBytes == 0)
00381 {
00382 exception = ServiceException(
00383 "input vector of bytes is empty",
00384 "makeProtobufStudyStatusControlFromBytes");
00385 return false;
00386 }
00387
00388 if (!protoStatusControl.ParseFromArray(&studyStatusAsBytes[0], numBytes))
00389 {
00390 exception = ServiceException(
00391 "ProtoBuf::Panel_StatusControl::ParseFromArray failed",
00392 "makeProtobufStudyStatusControlFromBytes");
00393 return false;
00394 }
00395
00396 return true;
00397 }
00398
00399 bool YosokumoProtobuf::makeStudyStatusFromProtobufStudyStatusControl(
00400 const ProtoBuf::Panel_StatusControl &protoStatusControl,
00401 Study::Status &status)
00402 {
00403 return protoStatusToStatus(protoStatusControl.status(), status);
00404 }
00405
00406
00407
00408
00409 bool YosokumoProtobuf::makeBytesFromStudyStatus(
00410 const Study::Status status,
00411 std::vector<uint8_t> &studyStatusAsBytes)
00412 {
00413 ProtoBuf::Panel_StatusControl protoStatusControl;
00414
00415 if (!makeProtobufStudyStatusControlFromStatus(status, protoStatusControl))
00416 return false;
00417
00418 return makeBytesFromProtobufStudyStatusControl(
00419 protoStatusControl, studyStatusAsBytes);
00420 }
00421
00422 bool YosokumoProtobuf::makeProtobufStudyStatusControlFromStatus(
00423 const Study::Status status,
00424 ProtoBuf::Panel_StatusControl &protoStatusControl)
00425 {
00426 ProtoBuf::Study_Status protoStatus;
00427
00428 if (!statusToProtobufStatus(status, protoStatus))
00429 return false;
00430
00431 protoStatusControl.set_status(protoStatus);
00432
00433 return true;
00434 }
00435
00436 bool YosokumoProtobuf::makeBytesFromProtobufStudyStatusControl(
00437 const ProtoBuf::Panel_StatusControl &protoStatusControl,
00438 std::vector<uint8_t> &studyStatusAsBytes)
00439 {
00440 int numBytes = protoStatusControl.ByteSize();
00441 studyStatusAsBytes.assign(numBytes, 0);
00442 return protoStatusControl.SerializeToArray(&studyStatusAsBytes[0], numBytes);
00443 }
00444
00445
00446
00447 bool YosokumoProtobuf::makeStudyVisibilityFromBytes(
00448 const std::vector<uint8_t> &studyVisibilityAsBytes,
00449 Study::Visibility &visibility)
00450 {
00451 ProtoBuf::Panel_VisibilityControl protoVisibilityControl;
00452
00453 if (!makeProtobufStudyVisibilityControlFromBytes(
00454 studyVisibilityAsBytes, protoVisibilityControl))
00455 return false;
00456
00457 return makeStudyVisibilityFromProtobufStudyVisibilityControl(
00458 protoVisibilityControl, visibility);
00459 }
00460
00461 bool YosokumoProtobuf::makeProtobufStudyVisibilityControlFromBytes(
00462 const std::vector<uint8_t> &studyVisibilityAsBytes,
00463 ProtoBuf::Panel_VisibilityControl &protoVisibilityControl)
00464 {
00465 int numBytes = int(studyVisibilityAsBytes.size());
00466
00467 if (numBytes == 0)
00468 {
00469 exception = ServiceException(
00470 "input vector of bytes is empty",
00471 "makeProtobufStudyVisibilityControlFromBytes");
00472 return false;
00473 }
00474
00475 if (!protoVisibilityControl.ParseFromArray(&studyVisibilityAsBytes[0],
00476 numBytes))
00477 {
00478 exception = ServiceException(
00479 "ProtoBuf::Panel_VisibilityControl::ParseFromArray failed",
00480 "makeProtobufStudyVisibilityControlFromBytes");
00481 return false;
00482 }
00483
00484 return true;
00485 }
00486
00487 bool YosokumoProtobuf::makeStudyVisibilityFromProtobufStudyVisibilityControl(
00488 const ProtoBuf::Panel_VisibilityControl &protoVisibilityControl,
00489 Study::Visibility &visibility)
00490 {
00491 return protoVisibilityToVisibility(protoVisibilityControl.visibility(),
00492 visibility);
00493 }
00494
00495
00496
00497
00498 bool YosokumoProtobuf::makeBytesFromStudyVisibility(
00499 const Study::Visibility visibility,
00500 std::vector<uint8_t> &studyVisibilityAsBytes)
00501 {
00502 ProtoBuf::Panel_VisibilityControl protoVisibilityControl;
00503
00504 if (!makeProtobufStudyVisibilityControlFromVisibility(visibility,
00505 protoVisibilityControl))
00506 return false;
00507
00508 return makeBytesFromProtobufStudyVisibilityControl(
00509 protoVisibilityControl, studyVisibilityAsBytes);
00510 }
00511
00512 bool YosokumoProtobuf::makeProtobufStudyVisibilityControlFromVisibility(
00513 const Study::Visibility visibility,
00514 ProtoBuf::Panel_VisibilityControl &protoVisibilityControl)
00515 {
00516 ProtoBuf::Study_Visibility protoVisibility;
00517
00518 if (!visibilityToProtobufVisibility(visibility, protoVisibility))
00519 return false;
00520
00521 protoVisibilityControl.set_visibility(protoVisibility);
00522
00523 return true;
00524 }
00525
00526 bool YosokumoProtobuf::makeBytesFromProtobufStudyVisibilityControl(
00527 const ProtoBuf::Panel_VisibilityControl &protoVisibilityControl,
00528 std::vector<uint8_t> &studyVisibilityAsBytes)
00529 {
00530 int numBytes = protoVisibilityControl.ByteSize();
00531 studyVisibilityAsBytes.assign(numBytes, 0);
00532 return protoVisibilityControl.SerializeToArray(&studyVisibilityAsBytes[0],
00533 numBytes);
00534 }
00535
00536
00537
00538 bool YosokumoProtobuf::makePanelFromBytes(
00539 const std::vector<uint8_t> &panelAsBytes,
00540 Panel &panel)
00541 {
00542 ProtoBuf::Panel protoPanel;
00543
00544 if (!makeProtobufPanelFromBytes(panelAsBytes, protoPanel))
00545 return false;
00546
00547 return makePanelFromProtobufPanel(protoPanel, panel);
00548
00549 }
00550
00551 bool YosokumoProtobuf::makeProtobufPanelFromBytes(
00552 const std::vector<uint8_t> &panelAsBytes,
00553 ProtoBuf::Panel &protoPanel)
00554 {
00555 int n = int(panelAsBytes.size());
00556
00557 if (n == 0)
00558 {
00559 exception = ServiceException(
00560 "input vector of bytes is empty",
00561 "makeProtobufPanelFromBytes");
00562 return false;
00563 }
00564
00565 if (!protoPanel.ParseFromArray(&panelAsBytes[0], n))
00566 {
00567 exception = ServiceException(
00568 "ProtoBuf::Panel::ParseFromArray failed",
00569 "makeProtobufPanelFromBytes");
00570 return false;
00571 }
00572
00573 return true;
00574
00575 }
00576
00577
00578 bool YosokumoProtobuf::makePanelFromProtobufPanel(
00579 const ProtoBuf::Panel &protoPanel,
00580 Panel &panel)
00581 {
00582 panel.setNameControlLocation(
00583 protoPanel.study_name_control().location());
00584 panel.setStatusControlLocation(
00585 protoPanel.status_control().location());
00586 panel.setVisibilityControlLocation(
00587 protoPanel.visibility_control().location());
00588
00589 panel.setBlockCount(
00590 protoPanel.block_count_control().block_count());
00591 panel.setCellCount(
00592 protoPanel.cell_count_control().cell_count());
00593 panel.setProspectCount(
00594 protoPanel.prospect_count_control().prospect_count());
00595
00596 panel.setCreationTime(
00597 protoPanel.creation_time_control().creation_time() );
00598 panel.setLatestBlockTime(
00599 protoPanel.latest_block_time_control().latest_block_time() );
00600 panel.setLatestProspectTime(
00601 protoPanel.latest_prospect_time_control().latest_prospect_time());
00602
00603 return true;
00604
00605 }
00606
00607
00608
00609
00610 bool YosokumoProtobuf::makeBytesFromPanel(
00611 const Panel &panel,
00612 std::vector<uint8_t> &panelAsBytes)
00613 {
00614 ProtoBuf::Panel protoPanel;
00615
00616 if (!makeProtobufPanelFromPanel(panel, protoPanel))
00617 return false;
00618
00619 return makeBytesFromProtobufPanel(protoPanel, panelAsBytes);
00620 }
00621
00622
00623 bool YosokumoProtobuf::makeProtobufPanelFromPanel(
00624 const Panel &panel,
00625 ProtoBuf::Panel &protoPanel)
00626 {
00627
00628
00629
00630
00631 protoPanel.set_study_identifier("study identifier");
00632 protoPanel.mutable_study_name_control()->set_study_name("study name");
00633 protoPanel.mutable_status_control() ->set_status(
00634 ProtoBuf::Study_Status_Stopped);
00635 protoPanel.mutable_visibility_control()->set_visibility(
00636 ProtoBuf::Study_Visibility_Private);
00637
00638
00639
00640 protoPanel.mutable_study_name_control()->set_location(
00641 panel.getNameControlLocation());
00642 protoPanel.mutable_status_control() ->set_location(
00643 panel.getStatusControlLocation());
00644 protoPanel.mutable_visibility_control()->set_location(
00645 panel.getVisibilityControlLocation());
00646
00647 protoPanel.mutable_block_count_control() ->set_block_count(
00648 panel.getBlockCount());
00649 protoPanel.mutable_cell_count_control() ->set_cell_count(
00650 panel.getCellCount());
00651 protoPanel.mutable_prospect_count_control()->set_prospect_count(
00652 panel.getProspectCount());
00653
00654 protoPanel.mutable_creation_time_control() ->set_creation_time(
00655 panel.getCreationTime());
00656 protoPanel.mutable_latest_block_time_control() ->set_latest_block_time(
00657 panel.getLatestBlockTime());
00658 protoPanel.mutable_latest_prospect_time_control()->set_latest_prospect_time(
00659 panel.getLatestProspectTime());
00660
00661 return true;
00662 }
00663
00664 bool YosokumoProtobuf::makeBytesFromProtobufPanel(
00665 const ProtoBuf::Panel &protoPanel,
00666 std::vector<uint8_t> &panelAsBytes)
00667 {
00668 int numBytes = protoPanel.ByteSize();
00669
00670 panelAsBytes.assign(numBytes, 0);
00671
00672 return protoPanel.SerializeToArray(&panelAsBytes[0], numBytes);
00673 }
00674
00675
00676
00677
00678 bool YosokumoProtobuf::makeRoleFromBytes(
00679 const std::vector<uint8_t> &roleAsBytes,
00680 Role &role)
00681 {
00682 ProtoBuf::Role protoRole;
00683
00684 if (!makeProtobufRoleFromBytes(roleAsBytes, protoRole))
00685 return false;
00686
00687 return makeRoleFromProtobufRole(protoRole, role);
00688
00689 }
00690
00691 bool YosokumoProtobuf::makeProtobufRoleFromBytes(
00692 const std::vector<uint8_t> &roleAsBytes,
00693 ProtoBuf::Role &protoRole)
00694 {
00695 int n = int(roleAsBytes.size());
00696
00697 if (n == 0)
00698 {
00699 exception = ServiceException(
00700 "input vector of bytes is empty",
00701 "makeProtobufRoleFromBytes");
00702 return false;
00703 }
00704
00705 if (!protoRole.ParseFromArray(&roleAsBytes[0], n))
00706 {
00707 exception = ServiceException(
00708 "ProtoBuf::Role::ParseFromArray failed",
00709 "makeProtobufRoleFromBytes");
00710 return false;
00711 }
00712
00713 return true;
00714
00715 }
00716
00717
00718 bool YosokumoProtobuf::makeRoleFromProtobufRole(
00719 const ProtoBuf::Role &protoRole,
00720 Role &role)
00721 {
00722 role.setUserIdentifier (protoRole.roleholder().user_identifier());
00723 role.setStudyIdentifier(protoRole.study().study_identifier());
00724
00725 role.setRoleLocation(protoRole.location());
00726 role.setUserName (protoRole.roleholder().user_name());
00727 role.setStudyName (protoRole.study().study_name());
00728
00729 ProtoBuf::Role_Privileges p = protoRole.privileges();
00730
00731 if (p.get_study()) role.addPrivilege(Privilege(Privilege::GET_STUDY ));
00732 if (p.delete_study()) role.addPrivilege(Privilege(Privilege::DELETE_STUDY));
00733 if (p.get_roster()) role.addPrivilege(Privilege(Privilege::GET_ROSTER ));
00734 if (p.post_roster()) role.addPrivilege(Privilege(Privilege::POST_ROSTER ));
00735 if (p.get_role()) role.addPrivilege(Privilege(Privilege::GET_ROLE ));
00736 if (p.put_role()) role.addPrivilege(Privilege(Privilege::PUT_ROLE ));
00737 if (p.delete_role()) role.addPrivilege(Privilege(Privilege::DELETE_ROLE ));
00738 if (p.get_panel()) role.addPrivilege(Privilege(Privilege::GET_PANEL ));
00739 if (p.get_control()) role.addPrivilege(Privilege(Privilege::GET_CONTROL ));
00740 if (p.put_control()) role.addPrivilege(Privilege(Privilege::PUT_CONTROL ));
00741 if (p.post_table()) role.addPrivilege(Privilege(Privilege::POST_TABLE ));
00742 if (p.get_model()) role.addPrivilege(Privilege(Privilege::GET_MODEL ));
00743 if (p.post_model()) role.addPrivilege(Privilege(Privilege::POST_MODEL ));
00744
00745 return true;
00746
00747 }
00748
00749
00750
00751
00752 bool YosokumoProtobuf::makeBytesFromRole(
00753 const Role &role,
00754 std::vector<uint8_t> &roleAsBytes)
00755 {
00756 ProtoBuf::Role protoRole;
00757
00758 if (!makeProtobufRoleFromRole(role, protoRole))
00759 return false;
00760
00761 return makeBytesFromProtobufRole(protoRole, roleAsBytes);
00762 }
00763
00764
00765 bool YosokumoProtobuf::makeProtobufRoleFromRole(
00766 const Role &role,
00767 ProtoBuf::Role &protoRole)
00768 {
00769 ProtoBuf::Role_Roleholder roleholder;
00770 roleholder.set_user_identifier(role.getUserIdentifier());
00771 roleholder.set_user_name (role.getUserName() );
00772
00773 ProtoBuf::Role_Study study;
00774 study.set_study_identifier(role.getStudyIdentifier());
00775 study.set_study_name (role.getStudyName() );
00776
00777 ProtoBuf::Role_Privileges p;
00778 p.set_get_study (role.getPrivilege(Privilege::GET_STUDY ));
00779 p.set_delete_study(role.getPrivilege(Privilege::DELETE_STUDY));
00780 p.set_get_roster (role.getPrivilege(Privilege::GET_ROSTER ));
00781 p.set_post_roster (role.getPrivilege(Privilege::POST_ROSTER ));
00782 p.set_get_role (role.getPrivilege(Privilege::GET_ROLE ));
00783 p.set_put_role (role.getPrivilege(Privilege::PUT_ROLE ));
00784 p.set_delete_role (role.getPrivilege(Privilege::DELETE_ROLE ));
00785 p.set_get_panel (role.getPrivilege(Privilege::GET_PANEL ));
00786 p.set_get_control (role.getPrivilege(Privilege::GET_CONTROL ));
00787 p.set_put_control (role.getPrivilege(Privilege::PUT_CONTROL ));
00788 p.set_post_table (role.getPrivilege(Privilege::POST_TABLE ));
00789 p.set_get_model (role.getPrivilege(Privilege::GET_MODEL ));
00790 p.set_post_model (role.getPrivilege(Privilege::POST_MODEL ));
00791
00792 protoRole.set_location(role.getRoleLocation());
00793 *protoRole.mutable_roleholder() = roleholder;
00794 *protoRole.mutable_study() = study;
00795 *protoRole.mutable_privileges() = p;
00796
00797 return true;
00798 }
00799
00800 bool YosokumoProtobuf::makeBytesFromProtobufRole(
00801 const ProtoBuf::Role &protoRole,
00802 std::vector<uint8_t> &roleAsBytes)
00803 {
00804 int numBytes = protoRole.ByteSize();
00805
00806 roleAsBytes.assign(numBytes, 0);
00807
00808 return protoRole.SerializeToArray(&roleAsBytes[0], numBytes);
00809 }
00810
00811
00812
00813 bool YosokumoProtobuf::makeRosterFromBytes(
00814 const std::vector<uint8_t> &rosterAsBytes,
00815 Roster &roster)
00816 {
00817 ProtoBuf::Roster protoRoster;
00818
00819 if (!makeProtobufRosterFromBytes(rosterAsBytes, protoRoster))
00820 return false;
00821
00822 return makeRosterFromProtobufRoster(protoRoster, roster);
00823
00824 }
00825
00826 bool YosokumoProtobuf::makeProtobufRosterFromBytes(
00827 const std::vector<uint8_t> &rosterAsBytes,
00828 ProtoBuf::Roster &protoRoster)
00829 {
00830 int n = int(rosterAsBytes.size());
00831
00832 if (n == 0)
00833 {
00834 exception = ServiceException(
00835 "input vector of bytes is empty",
00836 "makeProtobufRosterFromBytes");
00837 return false;
00838 }
00839
00840 if (!protoRoster.ParseFromArray(&rosterAsBytes[0], n))
00841 {
00842 exception = ServiceException(
00843 "ProtoBuf::Roster::ParseFromArray failed",
00844 "makeProtobufRosterFromBytes");
00845 return false;
00846 }
00847
00848 return true;
00849
00850 }
00851
00852
00853 bool YosokumoProtobuf::makeRosterFromProtobufRoster(
00854 const ProtoBuf::Roster &protoRoster,
00855 Roster &roster)
00856 {
00857 roster.setStudyIdentifier(protoRoster.study_identifier());
00858 roster.setStudyName (protoRoster.study_name() );
00859 roster.setRosterLocation (protoRoster.location() );
00860
00861 for (int i = 0; i < protoRoster.role_size(); ++i)
00862 {
00863 const ProtoBuf::Role &protoRole = protoRoster.role(i);
00864
00865 Role role;
00866
00867 if (!makeRoleFromProtobufRole(protoRole, role))
00868 return false;
00869
00870 if (!roster.addRole(role))
00871 {
00872 exception = ServiceException(
00873 "roster contains two roles with the same identifier",
00874 "makeRosterFromProtobufRoster");
00875
00876 return false;
00877 }
00878 }
00879
00880 return true;
00881
00882 }
00883
00884
00885
00886
00887 bool YosokumoProtobuf::makeBytesFromRoster(
00888 const Roster &roster,
00889 std::vector<uint8_t> &rosterAsBytes)
00890 {
00891 ProtoBuf::Roster protoRoster;
00892
00893 if (!makeProtobufRosterFromRoster(roster, protoRoster))
00894 return false;
00895
00896 return makeBytesFromProtobufRoster(protoRoster, rosterAsBytes);
00897 }
00898
00899
00900 bool YosokumoProtobuf::makeProtobufRosterFromRoster(
00901 const Roster &roster,
00902 ProtoBuf::Roster &protoRoster)
00903 {
00904 protoRoster.set_study_identifier(roster.getStudyIdentifier());
00905 protoRoster.set_study_name (roster.getStudyName() );
00906 protoRoster.set_location (roster.getRosterLocation() );
00907
00908 Roster::RoleConstIterator iter;
00909
00910 for (iter = roster.begin(); iter != roster.end(); ++iter)
00911 {
00912 const Role &role = iter->second;
00913
00914 ProtoBuf::Role *pProtoRole = protoRoster.add_role();
00915
00916 if (!makeProtobufRoleFromRole(role, *pProtoRole))
00917 return false;
00918 }
00919
00920 return true;
00921 }
00922
00923 bool YosokumoProtobuf::makeBytesFromProtobufRoster(
00924 const ProtoBuf::Roster &protoRoster,
00925 std::vector<uint8_t> &rosterAsBytes)
00926 {
00927 int numBytes = protoRoster.ByteSize();
00928
00929 rosterAsBytes.assign(numBytes, 0);
00930
00931 return protoRoster.SerializeToArray(&rosterAsBytes[0], numBytes);
00932 }
00933
00934
00935
00936 bool YosokumoProtobuf::makePredictorFromBytes(
00937 const std::vector<uint8_t> &predictorAsBytes,
00938 Predictor &predictor)
00939 {
00940 ProtoBuf::Predictor protoPredictor;
00941
00942 if (!makeProtobufPredictorFromBytes(predictorAsBytes, protoPredictor))
00943 return false;
00944
00945 return makePredictorFromProtobufPredictor(protoPredictor, predictor);
00946
00947 }
00948
00949 bool YosokumoProtobuf::makeProtobufPredictorFromBytes(
00950 const std::vector<uint8_t> &predictorAsBytes,
00951 ProtoBuf::Predictor &protoPredictor)
00952 {
00953 int n = int(predictorAsBytes.size());
00954
00955 if (n == 0)
00956 {
00957 exception = ServiceException(
00958 "input vector of bytes is empty",
00959 "makeProtobufPredictorFromBytes");
00960 return false;
00961 }
00962
00963 if (!protoPredictor.ParseFromArray(&predictorAsBytes[0], n))
00964 {
00965 exception = ServiceException(
00966 "ProtoBuf::Predictor::ParseFromArray failed",
00967 "makeProtobufPredictorFromBytes");
00968 return false;
00969 }
00970
00971 return true;
00972
00973 }
00974
00975
00976 bool YosokumoProtobuf::makePredictorFromProtobufPredictor(
00977 const ProtoBuf::Predictor &protoPredictor,
00978 Predictor &predictor)
00979 {
00980 predictor.setPredictorName (protoPredictor.name());
00981
00982 Predictor::Type type;
00983 if (!protoTypeToType(protoPredictor.type(), type))
00984 return false;
00985 predictor.setType(type);
00986
00987 Predictor::Status status;
00988 if (!protoStatusToStatus(protoPredictor.status(), status))
00989 return false;
00990 predictor.setStatus(status);
00991
00992 Predictor::Level level;
00993 if (!protoLevelToLevel(protoPredictor.level(), level))
00994 return false;
00995 predictor.setLevel(level);
00996
00997 return true;
00998
00999 }
01000
01001
01002
01003
01004 bool YosokumoProtobuf::makeBytesFromPredictor(
01005 const Predictor &predictor,
01006 std::vector<uint8_t> &predictorAsBytes)
01007 {
01008 ProtoBuf::Predictor protoPredictor;
01009
01010 if (!makeProtobufPredictorFromPredictor(predictor, protoPredictor))
01011 return false;
01012
01013 return makeBytesFromProtobufPredictor(protoPredictor, predictorAsBytes);
01014 }
01015
01016
01017 bool YosokumoProtobuf::makeProtobufPredictorFromPredictor(
01018 const Predictor &predictor,
01019 ProtoBuf::Predictor &protoPredictor)
01020 {
01021 protoPredictor.set_name(predictor.getPredictorName());
01022
01023 ProtoBuf::Predictor_Type type;
01024 if (!typeToProtobufType(predictor.getType(), type))
01025 return false;
01026 protoPredictor.set_type(type);
01027
01028 ProtoBuf::Predictor_Status status;
01029 if (!statusToProtobufStatus(predictor.getStatus(), status))
01030 return false;
01031 protoPredictor.set_status(status);
01032
01033 ProtoBuf::Predictor_Level level;
01034 if (!levelToProtobufLevel(predictor.getLevel(), level))
01035 return false;
01036 protoPredictor.set_level(level);
01037
01038 return true;
01039 }
01040
01041 bool YosokumoProtobuf::makeBytesFromProtobufPredictor(
01042 const ProtoBuf::Predictor &protoPredictor,
01043 std::vector<uint8_t> &predictorAsBytes)
01044 {
01045 int numBytes = protoPredictor.ByteSize();
01046
01047 predictorAsBytes.assign(numBytes, 0);
01048
01049 return protoPredictor.SerializeToArray(&predictorAsBytes[0], numBytes);
01050 }
01051
01052
01053
01054 bool YosokumoProtobuf::makeCellFromBytes(
01055 const std::vector<uint8_t> &cellAsBytes,
01056 Cell &cell)
01057 {
01058 ProtoBuf::Cell protoCell;
01059
01060 if (!makeProtobufCellFromBytes(cellAsBytes, protoCell))
01061 return false;
01062
01063 return makeCellFromProtobufCell(protoCell, cell);
01064
01065 }
01066
01067 bool YosokumoProtobuf::makeProtobufCellFromBytes(
01068 const std::vector<uint8_t> &cellAsBytes,
01069 ProtoBuf::Cell &protoCell)
01070 {
01071 int n = int(cellAsBytes.size());
01072
01073 if (n == 0)
01074 {
01075 exception = ServiceException(
01076 "input vector of bytes is empty",
01077 "makeProtobufCellFromBytes");
01078 return false;
01079 }
01080
01081 if (!protoCell.ParseFromArray(&cellAsBytes[0], n))
01082 {
01083 exception = ServiceException(
01084 "ProtoBuf::Cell::ParseFromArray failed",
01085 "makeProtobufCellFromBytes");
01086 return false;
01087 }
01088
01089 return true;
01090
01091 }
01092
01093
01094 bool YosokumoProtobuf::makeCellFromProtobufCell(
01095 const ProtoBuf::Cell &protoCell,
01096 Cell &cell)
01097 {
01098 uint64_t nameOrKey;
01099
01100 if (protoCell.has_key() ) nameOrKey = protoCell.key();
01101 else if (protoCell.has_name()) nameOrKey = protoCell.name();
01102 else
01103 {
01104 exception = ServiceException(
01105 "ProtoBuf::Cell has neither name nor key",
01106 "makeCellFromProtobufCell");
01107 return false;
01108 }
01109
01110 Value value;
01111 if (protoCell.has_empty() )
01112 value = EmptyValue();
01113 else if (protoCell.has_natural())
01114 value = NaturalValue(protoCell.natural());
01115 else if (protoCell.has_integer())
01116 value = IntegerValue(protoCell.integer());
01117 else if (protoCell.has_real())
01118 value = RealValue(protoCell.real());
01119 else if (protoCell.has_special())
01120 value = SpecialValue(protoCell.special());
01121 else
01122 {
01123 exception = ServiceException(
01124 "ProtoBuf::Cell has no value",
01125 "makeCellFromProtobufCell");
01126 return false;
01127 }
01128
01129 cell.setName(nameOrKey);
01130 cell.setValue(value);
01131
01132 return true;
01133
01134 }
01135
01136
01137
01138
01139 bool YosokumoProtobuf::makeBytesFromCell(
01140 const Cell &cell,
01141 std::vector<uint8_t> &cellAsBytes)
01142 {
01143 ProtoBuf::Cell protoCell;
01144
01145 if (!makeProtobufCellFromCell(cell, protoCell))
01146 return false;
01147
01148 return makeBytesFromProtobufCell(protoCell, cellAsBytes);
01149 }
01150
01151 bool YosokumoProtobuf::makeProtobufCellFromCell(
01152 const Cell &cell,
01153 ProtoBuf::Cell &protoCell)
01154 {
01155 protoCell.set_name(cell.getName());
01156
01157 Value v = cell.getValue();
01158
01159 switch (v.getType())
01160 {
01161 case Value::EMPTY: protoCell.set_empty(true); break;
01162 case Value::NATURAL: protoCell.set_natural(v.getNaturalValue()); break;
01163 case Value::INTEGER: protoCell.set_integer(v.getIntegerValue()); break;
01164 case Value::REAL: protoCell.set_real (v.getRealValue() ); break;
01165 case Value::SPECIAL: protoCell.set_special(v.getSpecialValue()); break;
01166 default:
01167 protoCell.set_empty(true);
01168 exception = ServiceException(
01169 "Yosokumo Cell value has unknown type",
01170 "makeProtobufCellFromCell");
01171 return false;
01172 }
01173
01174 return true;
01175 }
01176
01177 bool YosokumoProtobuf::makeBytesFromProtobufCell(
01178 const ProtoBuf::Cell &protoCell,
01179 std::vector<uint8_t> &cellAsBytes)
01180 {
01181 int numBytes = protoCell.ByteSize();
01182
01183 cellAsBytes.assign(numBytes, 0);
01184
01185 return protoCell.SerializeToArray(&cellAsBytes[0], numBytes);
01186 }
01187
01188
01189
01190
01191 bool YosokumoProtobuf::makeSpecimenFromBytes(
01192 const std::vector<uint8_t> &specimenAsBytes,
01193 Specimen &specimen)
01194 {
01195 ProtoBuf::Specimen protoSpecimen;
01196
01197 if (!makeProtobufSpecimenFromBytes(specimenAsBytes, protoSpecimen))
01198 return false;
01199
01200 return makeSpecimenFromProtobufSpecimen(protoSpecimen, specimen);
01201
01202 }
01203
01204 bool YosokumoProtobuf::makeProtobufSpecimenFromBytes(
01205 const std::vector<uint8_t> &specimenAsBytes,
01206 ProtoBuf::Specimen &protoSpecimen)
01207 {
01208 int n = int(specimenAsBytes.size());
01209
01210 if (n == 0)
01211 {
01212 exception = ServiceException(
01213 "input vector of bytes is empty",
01214 "makeProtobufSpecimenFromBytes");
01215 return false;
01216 }
01217
01218 if (!protoSpecimen.ParseFromArray(&specimenAsBytes[0], n))
01219 {
01220 exception = ServiceException(
01221 "ProtoBuf::Specimen::ParseFromArray failed",
01222 "makeProtobufSpecimenFromBytes");
01223 return false;
01224 }
01225
01226 return true;
01227
01228 }
01229
01230
01231 bool YosokumoProtobuf::makeSpecimenFromProtobufSpecimen(
01232 const ProtoBuf::Specimen &protoSpecimen,
01233 Specimen &specimen)
01234 {
01235 specimen.setSpecimenKey(protoSpecimen.key());
01236
01237 Specimen::Status status;
01238 if (!protoStatusToStatus(protoSpecimen.status(), status))
01239 return false;
01240 specimen.setStatus(status);
01241
01242 specimen.setWeight(protoSpecimen.weight());
01243
01244 Value value;
01245 if (protoSpecimen.has_empty() )
01246 value = EmptyValue();
01247 else if (protoSpecimen.has_natural())
01248 value = NaturalValue(protoSpecimen.natural());
01249 else if (protoSpecimen.has_integer())
01250 value = IntegerValue(protoSpecimen.integer());
01251 else if (protoSpecimen.has_real() )
01252 value = RealValue(protoSpecimen.real());
01253 else
01254 {
01255 exception = ServiceException(
01256 "ProtoBuf::Specimen has no value",
01257 "makeSpecimenFromProtobufSpecimen");
01258 return false;
01259 }
01260
01261 specimen.setPredictand(value);
01262
01263 for (int i = 0; i < protoSpecimen.cell_size(); ++i)
01264 {
01265 const ProtoBuf::Cell &protoCell = protoSpecimen.cell(i);
01266
01267 Cell cell;
01268
01269 if (!makeCellFromProtobufCell(protoCell, cell))
01270 return false;
01271
01272 specimen.addCell(cell);
01273 }
01274
01275 return true;
01276
01277 }
01278
01279
01280
01281
01282 bool YosokumoProtobuf::makeBytesFromSpecimen(
01283 const Specimen &specimen,
01284 std::vector<uint8_t> &specimenAsBytes)
01285 {
01286 ProtoBuf::Specimen protoSpecimen;
01287
01288 if (!makeProtobufSpecimenFromSpecimen(specimen, protoSpecimen))
01289 return false;
01290
01291 return makeBytesFromProtobufSpecimen(protoSpecimen, specimenAsBytes);
01292 }
01293
01294 bool YosokumoProtobuf::makeProtobufSpecimenFromSpecimen(
01295 const Specimen &specimen,
01296 ProtoBuf::Specimen &protoSpecimen)
01297 {
01298 protoSpecimen.set_key(specimen.getSpecimenKey());
01299
01300 ProtoBuf::Specimen_Status protoStatus;
01301 if (!statusToProtobufStatus(specimen.getStatus(), protoStatus))
01302 return false;
01303 protoSpecimen.set_status(protoStatus);
01304
01305 protoSpecimen.set_weight(specimen.getWeight());
01306
01307 Value v = specimen.getPredictand();
01308
01309 switch (v.getType())
01310 {
01311 case Value::EMPTY: protoSpecimen.set_empty(true); break;
01312 case Value::NATURAL: protoSpecimen.set_natural(v.getNaturalValue()); break;
01313 case Value::INTEGER: protoSpecimen.set_integer(v.getIntegerValue()); break;
01314 case Value::REAL: protoSpecimen.set_real (v.getRealValue()); break;
01315 default:
01316 protoSpecimen.set_empty(true);
01317 exception = ServiceException(
01318 "Yosokumo Specimen predictand value has unknown type",
01319 "makeProtobufSpecimenFromSpecimen");
01320 }
01321
01322 for (unsigned i = 0; i < specimen.size(); ++i)
01323 {
01324 const Cell &cell = specimen.getCell(i);
01325
01326 ProtoBuf::Cell *pProtoCell = protoSpecimen.add_cell();
01327
01328 if (!makeProtobufCellFromCell(cell, *pProtoCell))
01329 return false;
01330 }
01331
01332 return true;
01333 }
01334
01335
01336 bool YosokumoProtobuf::makeBytesFromProtobufSpecimen(
01337 const ProtoBuf::Specimen &protoSpecimen,
01338 std::vector<uint8_t> &specimenAsBytes)
01339 {
01340 int numBytes = protoSpecimen.ByteSize();
01341
01342 specimenAsBytes.assign(numBytes, 0);
01343
01344 return protoSpecimen.SerializeToArray(&specimenAsBytes[0], numBytes);
01345 }
01346
01347
01348
01349
01350 bool YosokumoProtobuf::makeBlockFromBytes(
01351 const std::vector<uint8_t> &blockAsBytes,
01352 Block &block)
01353 {
01354 ProtoBuf::Block protoBlock;
01355
01356 if (!makeProtobufBlockFromBytes(blockAsBytes, protoBlock))
01357 return false;
01358
01359 return makeBlockFromProtobufBlock(protoBlock, block);
01360
01361 }
01362
01363 bool YosokumoProtobuf::makeProtobufBlockFromBytes(
01364 const std::vector<uint8_t> &blockAsBytes,
01365 ProtoBuf::Block &protoBlock)
01366 {
01367 int n = int(blockAsBytes.size());
01368
01369 if (n == 0)
01370 {
01371 exception = ServiceException(
01372 "input vector of bytes is empty",
01373 "makeProtobufBlockFromBytes");
01374 return false;
01375 }
01376
01377 if (!protoBlock.ParseFromArray(&blockAsBytes[0], n))
01378 {
01379 exception = ServiceException(
01380 "ProtoBuf::Block::ParseFromArray failed",
01381 "makeProtobufBlockFromBytes");
01382 return false;
01383 }
01384
01385 return true;
01386
01387 }
01388
01389
01390 bool YosokumoProtobuf::makeBlockFromProtobufBlock(
01391 const ProtoBuf::Block &protoBlock,
01392 Block &block)
01393 {
01394 std::string id = protoBlock.study_identifier();
01395
01396 Block::Type blockType;
01397 if (protoBlock.has_empty() && protoBlock.empty())
01398 blockType = Block::EMPTY;
01399 else if (protoBlock.predictor_size() > 0)
01400 blockType = Block::PREDICTOR;
01401 else
01402 blockType = Block::CELL;
01403
01404
01405
01406
01407
01408
01409 switch (blockType)
01410 {
01411 case Block::EMPTY:
01412 block = EmptyBlock(id);
01413 break;
01414
01415 case Block::PREDICTOR:
01416 {
01417 block = PredictorBlock(id);
01418 PredictorBlock &pblock = (PredictorBlock&)block;
01419 for (int i = 0; i < protoBlock.predictor_size(); ++i)
01420 {
01421 const ProtoBuf::Predictor &protoPredictor = protoBlock.predictor(i);
01422
01423 Predictor predictor;
01424
01425 if (!makePredictorFromProtobufPredictor(protoPredictor, predictor))
01426 return false;
01427
01428 pblock.addPredictor(predictor);
01429 }
01430 break;
01431 }
01432
01433 case Block::CELL:
01434 {
01435 block = CellBlock(id);
01436 CellBlock &cblock = (CellBlock&)block;
01437 for (int i = 0; i < protoBlock.specimen_size(); ++i)
01438 {
01439 const ProtoBuf::Specimen &protoSpecimen = protoBlock.specimen(i);
01440
01441 Specimen specimen;
01442
01443 if (!makeSpecimenFromProtobufSpecimen(protoSpecimen, specimen))
01444 return false;
01445
01446 Cell cell(specimen.getSpecimenKey(), specimen.getPredictand());
01447
01448 cblock.addCell(cell);
01449 }
01450 break;
01451 }
01452
01453 default:
01454 assert(false);
01455 break;
01456 }
01457 return true;
01458
01459 }
01460
01461
01462
01463
01464 bool YosokumoProtobuf::makeBytesFromBlock(
01465 const Block &block,
01466 std::vector<uint8_t> &blockAsBytes)
01467 {
01468 ProtoBuf::Block protoBlock;
01469
01470 if (!makeProtobufBlockFromBlock(block, protoBlock))
01471 return false;
01472
01473 return makeBytesFromProtobufBlock(protoBlock, blockAsBytes);
01474 }
01475
01476 bool YosokumoProtobuf::makeProtobufBlockFromBlock(
01477 const Block &block,
01478 ProtoBuf::Block &protoBlock)
01479 {
01480 protoBlock.set_study_identifier(block.getStudyIdentifier());
01481
01482 switch (block.getType())
01483 {
01484 case Block::EMPTY:
01485 protoBlock.set_empty(true);
01486 break;
01487
01488 case Block::PREDICTOR:
01489 {
01490 protoBlock.clear_empty();
01491 PredictorBlock &pblock = (PredictorBlock&)block;
01492 for (unsigned i = 0; i < pblock.size(); ++i)
01493 {
01494 const Predictor &p = pblock.getPredictor(i);
01495 ProtoBuf::Predictor *pProtoPredictor = protoBlock.add_predictor();
01496 if (!makeProtobufPredictorFromPredictor(p, *pProtoPredictor))
01497 return false;
01498 }
01499 break;
01500 }
01501
01502 case Block::SPECIMEN:
01503 {
01504 protoBlock.clear_empty();
01505 SpecimenBlock &sblock = (SpecimenBlock&)block;
01506 for (unsigned i = 0; i < sblock.size(); ++i)
01507 {
01508 const Specimen *ps = sblock.getSpecimen(i);
01509 ProtoBuf::Specimen *pProtoSpecimen = protoBlock.add_specimen();
01510 if (!makeProtobufSpecimenFromSpecimen(*ps, *pProtoSpecimen))
01511 return false;
01512 }
01513 break;
01514 }
01515
01516 default:
01517 protoBlock.set_empty(true);
01518 exception = ServiceException(
01519 "Yosokumo::Block has unknown type",
01520 "makeProtobufBlockFromBlock");
01521 return false;
01522 }
01523
01524 return true;
01525 }
01526
01527 bool YosokumoProtobuf::makeBytesFromProtobufBlock(
01528 const ProtoBuf::Block &protoBlock,
01529 std::vector<uint8_t> &blockAsBytes)
01530 {
01531 int numBytes = protoBlock.ByteSize();
01532
01533 blockAsBytes.assign(numBytes, 0);
01534
01535 return protoBlock.SerializeToArray(&blockAsBytes[0], numBytes);
01536 }
01537
01538
01539
01540
01541
01542 bool YosokumoProtobuf::makeMessageFromBytes(
01543 const std::vector<uint8_t> &messageAsBytes,
01544 Message &message)
01545 {
01546 ProtoBuf::Message protoMessage;
01547
01548 if (!makeProtobufMessageFromBytes(messageAsBytes, protoMessage))
01549 return false;
01550
01551 return makeMessageFromProtobufMessage(protoMessage, message);
01552 }
01553
01554 bool YosokumoProtobuf::makeProtobufMessageFromBytes(
01555 const std::vector<uint8_t> &messageAsBytes,
01556 ProtoBuf::Message &protoMessage)
01557 {
01558 int n = int(messageAsBytes.size());
01559
01560 if (n == 0)
01561 {
01562 exception = ServiceException(
01563 "input vector of bytes is empty",
01564 "makeProtobufMessageFromBytes");
01565 return false;
01566 }
01567
01568 if (!protoMessage.ParseFromArray(&messageAsBytes[0], n))
01569 {
01570 exception = ServiceException(
01571 "ProtoBuf::Message::ParseFromArray failed",
01572 "makeProtobufMessageFromBytes");
01573 return false;
01574 }
01575
01576 return true;
01577
01578 }
01579
01580 bool YosokumoProtobuf::makeMessageFromProtobufMessage(
01581 const ProtoBuf::Message &protoMessage,
01582 Message &m)
01583 {
01584 m.setType(Message::INFORMATION);
01585 m.setText("");
01586
01587 Message::Type type;
01588
01589 if (!protoTypeToType(protoMessage.type(), type))
01590 return false;
01591
01592 std::string text = protoMessage.text();
01593 text = trim(text);
01594
01595
01596
01597
01598
01599
01600 if (text.empty())
01601 return false;
01602
01603 m.setType(type);
01604 m.setText(text);
01605
01606 return true;
01607
01608 }
01609
01610
01611
01612 bool YosokumoProtobuf::makeBytesFromMessage(
01613 const Message &message,
01614 std::vector<uint8_t> &messageAsBytes)
01615 {
01616 ProtoBuf::Message protoMessage;
01617
01618 if (!makeProtobufMessageFromMessage(message, protoMessage))
01619 return false;
01620
01621 return makeBytesFromProtobufMessage(protoMessage, messageAsBytes);
01622 }
01623
01624
01625 bool YosokumoProtobuf::makeProtobufMessageFromMessage(
01626 const Message &message,
01627 ProtoBuf::Message &protoMessage)
01628 {
01629 protoMessage.set_text(message.getText());
01630
01631 ProtoBuf::Message_Type type;
01632 if (!typeToProtobufType(message.getType(), type))
01633 return false;
01634 protoMessage.set_type(type);
01635
01636 return true;
01637 }
01638
01639 bool YosokumoProtobuf::makeBytesFromProtobufMessage(
01640 const ProtoBuf::Message &protoMessage,
01641 std::vector<uint8_t> &messageAsBytes)
01642 {
01643 int numBytes = protoMessage.ByteSize();
01644
01645 messageAsBytes.assign(numBytes, 0);
01646
01647 return protoMessage.SerializeToArray(&messageAsBytes[0], numBytes);
01648 }
01649
01650
01651
01652
01653
01654 bool YosokumoProtobuf::protoTypeToType(
01655 ProtoBuf::Study_Type protoType,
01656 Study::Type &type)
01657 {
01658 bool ok = true;
01659
01660 switch (protoType)
01661 {
01662 case ProtoBuf::Study_Type_Class: type = Study::CLASS; break;
01663 case ProtoBuf::Study_Type_Rank: type = Study::RANK; break;
01664 case ProtoBuf::Study_Type_Number: type = Study::NUMBER; break;
01665 case ProtoBuf::Study_Type_Chance: type = Study::CHANCE; break;
01666 default: ok = false; break;
01667 }
01668
01669 if (!ok)
01670 {
01671 exception = ServiceException(
01672 "ProtoBuf::Study_Type is invalid",
01673 "protoTypeToType: ProtoBuf::Study_Type -> Study::Type");
01674
01675 }
01676
01677 return ok;
01678
01679 }
01680
01681
01682
01683 bool YosokumoProtobuf::typeToProtobufType(
01684 Study::Type type,
01685 ProtoBuf::Study_Type &protoType)
01686 {
01687 bool ok = true;
01688
01689 switch (type)
01690 {
01691 case Study::CLASS : protoType = ProtoBuf::Study_Type_Class ; break;
01692 case Study::RANK : protoType = ProtoBuf::Study_Type_Rank ; break;
01693 case Study::NUMBER: protoType = ProtoBuf::Study_Type_Number; break;
01694 case Study::CHANCE: protoType = ProtoBuf::Study_Type_Chance; break;
01695 default: ok = false; break;
01696 }
01697
01698 if (!ok)
01699 {
01700 exception = ServiceException(
01701 "Study::Type is invalid",
01702 "typeToProtobufType: Study::Type -> ProtoBuf::Study_Type");
01703
01704 }
01705
01706 return ok;
01707
01708 }
01709
01710
01711
01712 bool YosokumoProtobuf::protoStatusToStatus(
01713 ProtoBuf::Study_Status protoStatus,
01714 Study::Status &status)
01715 {
01716 bool ok = true;
01717
01718 switch (protoStatus)
01719 {
01720 case ProtoBuf::Study_Status_Running: status = Study::RUNNING; break;
01721 case ProtoBuf::Study_Status_Standby: status = Study::STANDBY; break;
01722 case ProtoBuf::Study_Status_Stopped: status = Study::STOPPED; break;
01723 default: ok = false; break;
01724 }
01725
01726 if (!ok)
01727 {
01728 exception = ServiceException(
01729 "ProtoBuf::Study_Status is invalid",
01730 "protoStatusToStatus: ProtoBuf::Study_Status -> Study::Status");
01731
01732 }
01733
01734 return ok;
01735
01736 }
01737
01738
01739
01740 bool YosokumoProtobuf::statusToProtobufStatus(
01741 Study::Status status,
01742 ProtoBuf::Study_Status &protoStatus)
01743 {
01744 bool ok = true;
01745
01746 switch (status)
01747 {
01748 case Study::RUNNING: protoStatus = ProtoBuf::Study_Status_Running; break;
01749 case Study::STANDBY: protoStatus = ProtoBuf::Study_Status_Standby; break;
01750 case Study::STOPPED: protoStatus = ProtoBuf::Study_Status_Stopped; break;
01751 default: ok = false; break;
01752 }
01753
01754 if (!ok)
01755 {
01756 exception = ServiceException(
01757 "Study::Status is invalid",
01758 "statusToProtobufStatus: Study::Status -> ProtoBuf::Study_Status");
01759
01760 }
01761
01762 return ok;
01763
01764 }
01765
01766
01767
01768
01769 bool YosokumoProtobuf::protoVisibilityToVisibility(
01770 ProtoBuf::Study_Visibility protoVisibility,
01771 Study::Visibility &visibility)
01772 {
01773 bool ok = true;
01774
01775 switch (protoVisibility)
01776 {
01777 case ProtoBuf::Study_Visibility_Private: visibility = Study::PRIVATE; break;
01778 case ProtoBuf::Study_Visibility_Public: visibility = Study::PUBLIC; break;
01779 default: ok = false; break;
01780 }
01781
01782 if (!ok)
01783 {
01784 exception = ServiceException(
01785 "ProtoBuf::Study_Visibility is invalid",
01786 "protoVisibilityToVisibility: ProtoBuf::Study_Visibility -> "
01787 "Study::Visibility");
01788
01789 }
01790
01791 return ok;
01792
01793 }
01794
01795
01796
01797 bool YosokumoProtobuf::visibilityToProtobufVisibility(
01798 Study::Visibility visibility,
01799 ProtoBuf::Study_Visibility &protoVisibility)
01800 {
01801 bool ok = true;
01802
01803 switch (visibility)
01804 {
01805 case Study::PRIVATE: protoVisibility = ProtoBuf::Study_Visibility_Private; break;
01806 case Study::PUBLIC : protoVisibility = ProtoBuf::Study_Visibility_Public ; break;
01807 default: ok = false; break;
01808 }
01809
01810 if (!ok)
01811 {
01812 exception = ServiceException(
01813 "Study::Visibility is invalid",
01814 "visibilityToProtobufVisibility: Study::Visibility -> ProtoBuf::Study_Visibility");
01815
01816 }
01817
01818 return ok;
01819
01820 }
01821
01822
01823
01824
01825 bool YosokumoProtobuf::protoTypeToType(
01826 ProtoBuf::Message_Type protoType,
01827 Message::Type &type)
01828 {
01829 bool ok = true;
01830
01831 switch (protoType)
01832 {
01833 case ProtoBuf::Message_Type_Information: type = Message::INFORMATION; break;
01834 case ProtoBuf::Message_Type_Error: type = Message::ERROR; break;
01835 default: ok = false; break;
01836 }
01837
01838 if (!ok)
01839 {
01840 exception = ServiceException(
01841 "ProtoBuf::Message_Type is invalid",
01842 "protoTypeToType: ProtoBuf::Message_Type -> Message::Type");
01843 }
01844
01845 return ok;
01846
01847 }
01848
01849
01850
01851
01852 bool YosokumoProtobuf::typeToProtobufType(
01853 Message::Type type,
01854 ProtoBuf::Message_Type &protoType)
01855 {
01856 bool ok = true;
01857
01858 switch (type)
01859 {
01860 case Message::INFORMATION: protoType = ProtoBuf::Message_Type_Information; break;
01861 case Message::ERROR: protoType = ProtoBuf::Message_Type_Error; break;
01862 default: ok = false; break;
01863 }
01864
01865 if (!ok)
01866 {
01867 exception = ServiceException(
01868 "Message::Type is invalid",
01869 "typeToProtobufType: Message::Type -> ProtoBuf::Message_Type");
01870 }
01871
01872 return ok;
01873
01874 }
01875
01876
01877
01878
01879 bool YosokumoProtobuf::protoStatusToStatus(
01880 ProtoBuf::Predictor_Status protoStatus,
01881 Predictor::Status &status)
01882 {
01883 bool ok = true;
01884
01885 switch (protoStatus)
01886 {
01887 case ProtoBuf::Predictor_Status_Active: status = Predictor::ACTIVE; break;
01888 case ProtoBuf::Predictor_Status_Inactive: status = Predictor::INACTIVE; break;
01889 default: ok = false; break;
01890 }
01891
01892 if (!ok)
01893 {
01894 exception = ServiceException(
01895 "ProtoBuf::Predictor_Status is invalid",
01896 "protoStatusToStatus: ProtoBuf::Predictor_Status -> Predictor::Status");
01897
01898 }
01899
01900 return ok;
01901
01902 }
01903
01904
01905
01906
01907 bool YosokumoProtobuf::statusToProtobufStatus(
01908 Predictor::Status status,
01909 ProtoBuf::Predictor_Status &protoStatus)
01910 {
01911 bool ok = true;
01912
01913 switch (status)
01914 {
01915 case Predictor::ACTIVE: protoStatus = ProtoBuf::Predictor_Status_Active; break;
01916 case Predictor::INACTIVE: protoStatus = ProtoBuf::Predictor_Status_Inactive; break;
01917 default: ok = false; break;
01918 }
01919
01920 if (!ok)
01921 {
01922 exception = ServiceException(
01923 "Predictor::Status is invalid",
01924 "statusToProtobufStatus: Predictor::Status -> ProtoBuf::Predictor_Status");
01925
01926 }
01927
01928 return ok;
01929
01930 }
01931
01932
01933
01934 bool YosokumoProtobuf::protoTypeToType(
01935 ProtoBuf::Predictor_Type protoType,
01936 Predictor::Type &type)
01937 {
01938 bool ok = true;
01939
01940 switch (protoType)
01941 {
01942 case ProtoBuf::Predictor_Type_Categorical: type = Predictor::CATEGORICAL; break;
01943 case ProtoBuf::Predictor_Type_Continuous: type = Predictor::CONTINUOUS; break;
01944 default: ok = false; break;
01945 }
01946
01947 if (!ok)
01948 {
01949 exception = ServiceException(
01950 "ProtoBuf::Predictor_Type is invalid",
01951 "protoTypeToType: ProtoBuf::Predictor_Type -> Predictor::Type");
01952 }
01953
01954 return ok;
01955
01956 }
01957
01958
01959
01960
01961 bool YosokumoProtobuf::typeToProtobufType(
01962 Predictor::Type type,
01963 ProtoBuf::Predictor_Type &protoType)
01964 {
01965 bool ok = true;
01966
01967 switch (type)
01968 {
01969 case Predictor::CATEGORICAL: protoType = ProtoBuf::Predictor_Type_Categorical; break;
01970 case Predictor::CONTINUOUS: protoType = ProtoBuf::Predictor_Type_Continuous; break;
01971 default: ok = false; break;
01972 }
01973
01974 if (!ok)
01975 {
01976 exception = ServiceException(
01977 "Predictor::Type is invalid",
01978 "typeToProtobufType: Predictor::Type -> ProtoBuf::Predictor_Type");
01979 }
01980
01981 return ok;
01982
01983 }
01984
01985
01986
01987
01988 bool YosokumoProtobuf::protoLevelToLevel(
01989 ProtoBuf::Predictor_Level protoLevel,
01990 Predictor::Level &level)
01991 {
01992 bool ok = true;
01993
01994 switch (protoLevel)
01995 {
01996 case ProtoBuf::Predictor_Level_Nominal: level = Predictor::NOMINAL; break;
01997 case ProtoBuf::Predictor_Level_Ordinal: level = Predictor::ORDINAL; break;
01998 case ProtoBuf::Predictor_Level_Interval: level = Predictor::INTERVAL; break;
01999 case ProtoBuf::Predictor_Level_Ratio: level = Predictor::RATIO; break;
02000 default: ok = false; break;
02001 }
02002
02003 if (!ok)
02004 {
02005 exception = ServiceException(
02006 "ProtoBuf::Predictor_Level is invalid",
02007 "protoLevelToLevel: ProtoBuf::Predictor_Level -> Predictor::Level");
02008 }
02009
02010 return ok;
02011
02012 }
02013
02014
02015
02016
02017 bool YosokumoProtobuf::levelToProtobufLevel(
02018 Predictor::Level level,
02019 ProtoBuf::Predictor_Level &protoLevel)
02020 {
02021 bool ok = true;
02022
02023 switch (level)
02024 {
02025 case Predictor::NOMINAL: protoLevel = ProtoBuf::Predictor_Level_Nominal; break;
02026 case Predictor::ORDINAL: protoLevel = ProtoBuf::Predictor_Level_Ordinal; break;
02027 case Predictor::INTERVAL: protoLevel = ProtoBuf::Predictor_Level_Interval; break;
02028 case Predictor::RATIO: protoLevel = ProtoBuf::Predictor_Level_Ratio; break;
02029 default: ok = false; break;
02030 }
02031
02032 if (!ok)
02033 {
02034 exception = ServiceException(
02035 "Predictor::Level is invalid",
02036 "levelToProtobufLevel: Predictor::Level -> ProtoBuf::Predictor_Level");
02037 }
02038
02039 return ok;
02040
02041 }
02042
02043
02044
02045
02046 bool YosokumoProtobuf::protoStatusToStatus(
02047 ProtoBuf::Specimen_Status protoStatus,
02048 Specimen::Status &status)
02049 {
02050 bool ok = true;
02051
02052 switch (protoStatus)
02053 {
02054 case ProtoBuf::Specimen_Status_Active: status = Specimen::ACTIVE; break;
02055 case ProtoBuf::Specimen_Status_Inactive: status = Specimen::INACTIVE; break;
02056 default: ok = false; break;
02057 }
02058
02059 if (!ok)
02060 {
02061 exception = ServiceException(
02062 "ProtoBuf::Specimen_Status is invalid",
02063 "protoStatusToStatus: ProtoBuf::Specimen_Status -> Specimen::Status");
02064 }
02065
02066 return ok;
02067
02068 }
02069
02070
02071
02072 bool YosokumoProtobuf::statusToProtobufStatus(
02073 Specimen::Status status,
02074 ProtoBuf::Specimen_Status &protoStatus)
02075 {
02076 bool ok = true;
02077
02078 switch (status)
02079 {
02080 case Specimen::ACTIVE: protoStatus = ProtoBuf::Specimen_Status_Active; break;
02081 case Specimen::INACTIVE: protoStatus = ProtoBuf::Specimen_Status_Inactive; break;
02082 default: ok = false; break;
02083 }
02084
02085 if (!ok)
02086 {
02087 exception = ServiceException(
02088 "Specimen::Status is invalid",
02089 "statusToProtobufStatus: Specimen::Status -> ProtoBuf::Specimen_Status");
02090 }
02091
02092 return ok;
02093
02094 }
02095
02096
02097